Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 

How to run nodejs express snap application

How to run nodejs express snap application

jawad
Long-established Member

I have nodejs snap application, in which express server is used, 
here is express app.js code

#!/usr/bin/env node

const express = require('express');
const app = express();
const port = 3000;

// Define a route for the root URL
app.get('/', (req, res) => {
  res.send('Welcome to our Node.js Express app !');
});

// Define a route to get a list of items
app.get('/items', (req, res) => {
  const items = ['Item 1', 'Item 2', 'Item 3'];
  res.json(items);
});

// Start the server
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});\
 
now i install snap application on vm ctrlx works in service mode, how i can run the project and how i can 

http://localhost:3000 

10 REPLIES 10

MauroRiboniMX
Contributor

Hello,

If your snap has this app set as a daemon the process is started at boot.
Regarding Express JS: you have to start from an example and modify it in order to achieve your result.

Mauro

jawad
Long-established Member

here is my snapcraft.yaml 

jawad_0-1698325827685.png

what changes should i do it? 
and i have installed it on ctrlx works 

jawad_1-1698325902355.png

 

Sgilk
Frequent Contributor

It looks like the app is installed correctly. Can you not access your routes?

Ex. http://localhost:3000/items

If after reboot and switching the control into operating mode you still cannot access, there may be a problem with the app.

You can view the status & logs over SSH in the virtual CORE. Under device settings in ctrlX Works, select this option.

Sgilk_0-1698326909389.png

Then, log into the QEMU emulator using the credentials below.

     Username: rexroot

     Password: rexroot

Run the commands below to find your app name and view the snap logs.

     sudo snap list

     sudo snap logs app_name

Additional information can be found by

    sudo systemctl status

    sudo journalctl

Hello,

The address should be http://192.168.1.1:3000.. not localhost. Used in port forwarding would be http://localhost:3000.. but you have to forward the right port.

Mauro

jawad
Long-established Member

currenty i am doing what you mentioned above. but it doesnot works, anything else should i do?

jawad_1-1698381880123.png

jawad_2-1698381900386.png

 

 

jawad
Long-established Member

i check log book , but i donot what are these issues and how i can solve it.

jawad_0-1698384193860.png

 

Sgilk
Frequent Contributor

The node-project.app is failing to start. At this point, I'd definitely suggest trying to debug the app using the logging tools I provided in my last comment. The log book does not necessarily provide all relevant information.

jawad
Long-established Member

This code works fine with express server installed and imported, but api are commented. 

#!/usr/bin/env node
// !!! DO NOT REMOVE THE SHEBANG ON TOP OF THE FILE, WHICH SPECIFIES THIS APP TO BE EXECUTED BY NODE.JS !!!
    const express = require('express');
    const app = express();
    const port = 3000;

    // app.get('/', (req, res) => {
    // res.send('Hello, Express! Exd jawad');
    // });

    // app.listen(port, () => {
    // console.log(`Server is listening on port exdddd ${port}`);
    // });
    // // Define a route to get a list of items
    // app.get('/items', (req, res) => {
    //     const items = ['Item 1', 'Item 2', 'Item 3'];
    //     res.json(items);
    // });
   
    function helloWorld() {
        console.log("EXD expresss 11")
        console.log('app ====', app)
        console.log('port ====', port)
        console.log("EXD expresss 22")
 
    };

    helloWorld();

but when i un-comment the api lines error occurs in log book which i mentioned above in earlier reply.
So, I think if there is problem in code or application e.g (problem in express server) then basic code with comment should not works. because i have should express related things in console.log
but it shows me express related function in logbook. 
jawad_3-1698662431381.pngjawad_4-1698662452329.png

 

 


jawad_0-1698662371541.pngjawad_1-1698662385123.pngjawad_5-1698662496440.png

 

 

Sgilk
Frequent Contributor

I agree there is a problem and that is why I suggest debugging over SSH as described above. My guess is a problem importing the express library, but it is difficult to know without better error messages. The logbook logs do not contain all possible information.

CodeShepherd
Community Moderator
Community Moderator

@jawad Are there any news? Could this issue been solved?

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist