8 Replies Latest reply on Jun 13, 2015 5:26 AM by Koen.Bruyninckx

    Start a nodejs server on boot of edison

    eco_bach

      For a current project I will need to auto run a nodejs server when the edison boots. Does anyone know if this can be done via configure_edison --setup  ?

        • 1. Re: Start a nodejs server on boot of edison
          intel_dan

          If you are using the Intel XDK IoT Edition, the last loaded app will load at startup (aka what ever app is in the '/node_app_slot' directory). Even if you are not using the Intel XDK IoT Edition, placing a node.js app in  '/node_app_slot'  will make it run at startup.  If you would like to make an app run at startup outside of the XDK mechanism, I recommend creating a systemd service (google 'systemd service' or use tutorials such as Getting Started with systemd)

          • 3. Re: Start a nodejs server on boot of edison
            mhahn

            intel_dan wrote:

             

            [...] I recommend creating a systemd service (google 'systemd service' or use tutorials such as Getting Started with systemd)

            you could also start inspecting the existing systemd service files /lib/systemd/system/*.service

            • 4. Re: Start a nodejs server on boot of edison
              joshuajnoble

              If I've placed a file in /node_app_slot is there anything special that needs to be in it for it to start up at boot? I've placed a very simple node server that listens for Serial data in /node_app_slot and it isn't started on boot. I've also tried doing init.d scripts and systemd services to no avail. Any suggestions?

              • 5. Re: Start a nodejs server on boot of edison
                intel_dan

                you need to have a package.json file with the 'main' js file specified. If the XDK daemon is disabled on you system, you will need to type 'systemctl enable xdk-daemon' and 'systemctl start xdk-daemon' to start the daemon.

                 

                below is a example of a minimal package.json (notice the 'main' field that specifies the 'entry point' into the node.js app you want to run)

                 

                 

                {

                  "name": "sample",

                  "description": "",

                  "version": "0.0.0",

                  "main": "main.js",

                  "engines": {

                    "node": ">=0.10.0"

                  },

                  "dependencies": {

                  }

                }

                • 6. Re: Start a nodejs server on boot of edison
                  joshuajnoble

                  Thanks for response Dan, I'll give that a look when I'm back in the office on Monday.

                  • 7. Re: Start a nodejs server on boot of edison
                    Koen.Bruyninckx

                    Hi guys,

                    For starters, thanks for the help people are trying to give, but up till now, I still didn't manage to start my node script at boot.

                    I have read the documentation on systemd, wrote a simple service:

                    [Unit]

                    Description=MyTestApp

                    After=xdk-daemon.service

                    Requires=xdk-daemon.service

                     

                    [Service]

                    #User=root

                    TimeoutStartSec=10

                    ExecStart=/usr/bin/node /home/root/shellScript.js

                     

                    [Install]

                    WantedBy=multi-user.target

                     

                    I then performed the following commands:

                    systemctl enable /etc/systemd/system/myTestApp.service

                    even to check whether it works:

                    systemctl start myTestApp.service

                    But it simply won't work

                     

                    Why is there no decent step by step documentation to do a thing that should be so elementary?  Why do I have to read every single post in multiple threads just to make sure that I didn't miss anything.  I am spending 2 days now on a simple motion detected triggering wave producing application, which in my opinion should be a walk in the park, but, no, not with edison...

                    • 8. Re: Start a nodejs server on boot of edison
                      Koen.Bruyninckx

                      OK, sorry for my part of documentation frustration...

                      I found my issue by reading the system log:

                      journalctl -u myTestApp.service

                       

                      It appeared that the node dependencies were not correctly met:

                      The following lines were necessary in my service:

                      [Service]

                      Environment="NODE_PATH=/usr/lib/node_modules"

                      Environment="NODE_ENV=production"

                      Environment="AVAHI_COMPAT_NOWARN=1"

                      Environment="HOME=/home/root"

                       

                      Once I have everything working, I will write a guideline so that people do not have to read every single post