Lionel T Lionel T - 2 months ago 17
Node.js Question

NodeJS API with external deps in other language

I am developing a

NodeJS
API and everything is ok.

For an specific issue I am using a local CLI dependency that process some input files and output other stuff, in case to return from the API.

I wanted to know (maybe open my mind on) what kind of service I can use to serve this API in production.

The idea is to have a
Node
environment (like in my local) that can have installed in the same machine an external dependency not necessarily written in
Node
.

My specific dependency is fontforge and other little things.

Thanks in advance.

rsp rsp
Answer

It's hard to beat a good VPS if you need to install custom software that is not easy to install with npm. My favorite VPS provider is Digital Ocean. You can have two months of a basic server for free with this link so you can see if it's ok for you before you pay anything. But it will mean taking care of the server yourself. With services like Heroku all of that is taken care for you - but you can't install whatever you want there. With a VPS you get your own server with root access. Usually it's Linux but Digital Ocean also supports FreeBSD and some people install OpenBSD, though it's not officially supported. With a VPS you can install whatever you want, but you have to do it yourself. There is always a trade off.

More info

Installing Node

To install Node on the VPS, my recommendation is to install in /opt with a versioned directory and a symlink - this is an example procedure that I wrote for a different answer:

# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node

See this answer for more info.

Your start scripts

To have your own application nicely started on server startup - here is an example Upstart script based on the one that I'm using - it should work on Ubuntu 14.04, not tested on newer versions - save it in /etc/init/YOURAPP.conf:

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [06]

# If the process quits unexpectadly trigger a respawn
respawn

# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1

Just change:

  • YOURAPP to the name of your own app
  • /opt/node/bin/node to your path to node
  • /www/YOURAPP/app/app.js to the path of your Node app
  • /www/YOURAPP/run to where you want your PID file
  • /www/YOURAPP/log to where you want your logs
  • --chuid node to --chuid OTHERUSER if you want it to run as a different user than node

(make sure to add a user with a name from --chuid above)

With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run:

start YOURAPP
restart YOURAPP
stop YOURAPP

to start, restart and stop your app - which would also happen automatically during the system boot or shutdown.