Node.js on a Synology box

I’ve got a Synology box sitting somewhere on my network that I use for general file service. To put it to even more use I’ve installed a NodeJS server on it. Here’s the basics of how to do that.

1. Prep the NAS

I did most of this previously for other things that I was using my NAS box for. There are two basic steps:

  1. Install the NodeJS package. I used the Package Manager and installed v4. It seemed like low-hanging fruit.
  2. Enable and stabilize an ssh connection to the NAS. If this isn’t something that you can figure out how to do on your own or you’re afraid to do it, you probably shouldn’t be doing any of this.

At the end of this prep, NodeJS should be installed and you should be able to ssh to the NAS. Check to see that the account you’re using can issue sudo commands because you’re going to need that.

I probably opened a port range somewhere at some point while I was installing the Web Service package and getting other development sites up and running on that. We’ll probably see more about this later.

I will leave it as an exercise to the reader to figure out where they want to put their application files. I suppose it would be possible to put them anywhere on the file system. I chose somewhere under a virtual directory on my drive array volume (/volume1, for me) so that I could access them more easily from other places using the DSM GUI tools.

2. Create your project

It seems that the best way to do this is with node init. Look here for a good doc on that.

3. Create an Upstart service

The version of DSM that I have installed at the time of writing this has Upstart installed by default. The upside to using Upstart is that it’s fully integrated with the DSM software. The downside is that in order to add a service you have to add it in /etc/init. That directory will likely get overwritten every time the DSM updates. Make sure to save a copy of your .conf file after you create it, somewhere other than in /etc/init.

I found an excellent tutorial here about how to set this all up.

In the future, I hope to upgrade this to a DSM package. This assumes that it’s viable to create a bespoke package that I can store and update without publishing it for the whole world. Also, if I’m going to do this I’ll probably create a NodeJS service package that will handle any arbitrary installations instead of a single one for just one application. I think these things are written in Python, which I don’t know very well.

4. Try your service

This is as simple as sudo start your-service-name. If that runs, it should run every time the NAS reboots. If you wrote index.js correctly, you should be able to browse to the correct port on your NAS box’s address and see your application.

5. Upgrade to forever

My app code directory is mounted to my local machine via NFS. I wanted one of those nifty file system watchers that automatically recompiles my code on change. I tried nodemon but it didn’t seem to work. I had better luck with forever.

Use sudo npm install -g forever. It will probably give you an error about the fsevents dependency. Ignore this, it’s a package for Darwin (Mac OS) only and the same functionality seems to be implemented elsewhere within either forever or its dependencies for Linux (which is what my Synology NAS reports it’s running on).

This caused me to change the command line in my service from node to forever. I used a JSON configuration file for my application.

Looking at this, if I ever do write a service package, it seems like this would be a great way to do it. It seems that you can start multiple services with a single JSON file and configure each separately. The package could probably just write the JSON and restart the service.

4. Try your service

This is as simple as sudo start your-service-name. If that runs, it should run every time the NAS reboots. If you wrote index.js correctly, you should be able to browse to the correct port on your NAS box’s address and see your application.

5. Upgrade to forever

My app code directory is mounted to my local machine via NFS. I wanted one of those nifty file system watchers that automatically recompiles my code on change. I tried nodemon but it didn’t seem to work. I had better luck with forever.

Use sudo npm install -g forever. It will probably give you an error about the fsevents dependency. Ignore this, it’s a package for Darwin (Mac OS) only and the same functionality seems to be implemented elsewhere within either forever or its dependencies for Linux (which is what my Synology NAS reports it’s running on).

This caused me to change the command line in my service from node to forever. I used a JSON configuration file for my application.

Looking at this, if I ever do write a service package, it seems like this would be a great way to do it. It seems that you can start multiple services with a single JSON file and configure each separately. The package could probably just write the JSON and restart the service.

More to come

I’ve got a lot more detail for each of these operations and other things I need to install. Some targets:

  • I want to install something like Broccoli that will help my build process. I may end up with Grunt instead.
  • I’ve got some work to do with keeping my local machine where I do my development in sync with the development environment on the NAS.
  • Once that’s done I should be able to get through installing some packages that will help with getting my site up and running quickly. The application I’m working on right now is probably going to be written in VueJS.
  • I’ve got some configuration of the service with a router.

 

Leave a Reply

Your email address will not be published. Required fields are marked *