NodeJS API with external deps in other language
Asked Answered
R

1

0

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.

Ronaldronalda answered 22/9, 2016 at 9:32 Comment(0)
G
1

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. By second favorite VPS provider is Vultr because you can install custom ISOs on their servers. You can try it for free with this link. 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.

Gwyn answered 22/9, 2016 at 10:24 Comment(6)
great! @Gwyn thank you for the detailed reply. I thought about DO but didn't tried yet, thank you also for the link! So in this case I'll have to install node and initialise all the processes to make it work the API like it works in heroku and other, right?Ronaldronalda
@LionelT I updated my answer with info on how to install Node and how to start your application automatically.Gwyn
@LionelT I forgot about one thing - the above upstart script assumes that you have a user named node in your system - to run your app as that user - make sure that you either have a user named like this or change it to some other user. I added it to the answer as well.Gwyn
awesome! can I ask you how much droplets you have in DO?Ronaldronalda
@LionelT just a few. it's surprising how much stuff you can host on one droplet. there are no limits of domains, websites or anything - just the storage limit. I've even heard that they don't enforce the traffic limit but even if they did it's in terabytes, so you can actually host a lot on one little droplet. For services like Heroku, it's at most one app per instance but on Digital Ocean it's as much as you can fit in RAM and on disk. By the way, all disks are SSD so it's great for performance.Gwyn
ok I'm in DigialOcean, thank you again for all the info @rsp, can I ask you what is a better way to handle an API and an App (that calls also this API) in the same VPS? create at least two different droplets (with different IP)? Could be problematic to have all in the same droplet (even one bigger)? Thanks in advance again,Ronaldronalda

© 2022 - 2024 — McMap. All rights reserved.