3

The Nas I used at home has installed Node JS v12 from Synology's package center. So I thought it would be cool to create a Node JS http webserver and study myself into back-end and front end development.

Create volume1/path_to_your_app/app.js and let the http server listen to port 8080. Next, With Powershell I could login with my Synology (admin) account's [username] and [password] into the Nas through ip-address from home. For accessing with ssh in Synology's cofiguration/Terminal the ssh-service must be checked. The default portnumber is 22, I changed this to for example 8822. Next, from Powershell I could now run the command:

  • ssh username@192.168.1.44 -p8822
  • Next password will be prompted

From Synology's shell I could now navigate to the file, run it and view it in my browser at 192.168.1.44:8080/.

  • cd /volume1/path_to_your_app/
  • node app.js

Next I setup port forwarding to host the webserver online. In my routers settings/port forwarding ipv4/ my nas is defined under an ip-address 192.168.1.44. At the Nas's application configuration I added a new rule under TCP protocol on port 8080. It only works for port 8080 (as far as I could get it workingfor now) because this is an experimental port. I could now access my webserver on my router's WAN-IP:8080/. After I exit the Synology's shell (ctrl+c) my application also stops running. I was not able to install forever from npm to keep the server running. However from this page I found a solution. I had to create an upstart file from Synology's shell in the /etc/init folder.

  • cd / (go back to root)
  • cd /etc/init/
  • sudo vi yourappname.conf

  • the I copy/pasted this code in the (.conf) file AND editted the parameters to my enviroment:

#!upstart
description "your app name"
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env NODE_ENV=development
# Warning: this runs node as root user, which is a security risk
# in many scenarios, but upstart-ing a process as a non-root user
# is outside the scope of this question

chdir /volume1/path_to_your_app/
exec node /volume1/path_to_your_app/app.js >> /var/log/yourappname.log 2>&1

Now I could start and stop the upstart script manually from Synology's shell and it keeps on running even after I exit the Synology's shell. I also inserted "chdir /path_to_your_app/" in the script before "exec" because it changes the directory from where Node JS is running to the directory from your app. I don't know what security risks are from the warning in the script.

  • sudo start yourappname
  • sudo stop yourappname

Debugging my Node JS application i do with the commands:

  • sudo tail -f /var/log/yourappname.log
Berend Kemper
  • 31
  • 1
  • 5
  • 1
    It would be a good answer to the question "How can I run a persistent node.js application on a synology nas?" – NilsB Feb 04 '21 at 14:50
  • 1
    Hey thanks for your interest! You are right i did not ask a question. With this post i meant to document how i managed for my first time to execute code with a Node JS enviroment and all of it's native libraries. However i did not mention soon after i realised that Synology blocks me from installing any external libraries from NPM in Node JS from Synology's package center. So i installed Docker from Synology's package center and managed to create two images from Node JS and MongoDB. On the Docker image from Node JS i was finally able to install MongoDB from NPM (which is an external library). – Berend Kemper Feb 21 '21 at 19:26
  • Hello, thank you for the answer. How can I modify this config so that service also starts on machine restart? – tomitrescak Jul 09 '21 at 22:45
  • Save an upstart script in a .conf file in /etc/init (if you need it to run as root when the system boots up) https://askubuntu.com/a/910 To tell you ther truth, i did not get this to work on my Synology. However i've been using Docker and Docker has restart policies: 1. https://docs.docker.com/config/containers/start-containers-automatically/ 2. docker run --restart: https://docs.docker.com/engine/reference/commandline/run/ 3. docker-compose.yml: https://docs.docker.com/compose/compose-file/compose-file-v3/#restart – Berend Kemper Jul 11 '21 at 14:58
  • Does the synology NAS does not have a "Application" or "Startup" setting? I dide the same with node on my synology nas and the application started automaticly. I dont rembember exectly where, but i know that is was configruable through the GUI. Startup task or Applications... There you should be able to start your app automaticly on startup. – Marc Jul 02 '22 at 23:33

1 Answers1

0

I don't know why you wrote the Warning because it can be solved by one command setuid <username>

UncleVic
  • 3
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 19 '22 at 12:28