Building a simple Node.js server on scaleway

This tutorial will help build a simple hosted Node.js server using scaleway in just a few minutes.
We are going to use a pre-built server image from scaleway and a Node project from a Github repo. I have written this post to remind myself,
but hopefully it will be useful to others.

Clone your Node repo from GitHub. The space and word "app" at the end of the command tells git to place
the project contents into a new directory called "app"

$ git clone https://github.com/glennjones/hapi-bootstrap.git app

Move into the new app directory

$ cd app

Install Node packages

$ npm install

Setup the basic environmental properties. Note your app needs to use process.env.HOST and process.env.PORT
to set its network connection

$ export PORT=80
$ export HOST=0.0.0.0
$ export NODE_ENV=production

Running Node server

Rather than running Node directly we will use PM2. It has two major advantagaes to running Node directly, first is PM2 deamon
that keeps your app alive forever, reloading it when required. The second is that PM2 will manage Node's cluster features, running
a Node instance on multiple cores, bringing them together to act as one service.

$ pm2 start app.js -i 0

The -i 0 flag tells PM2 to run in cluster mode and use as many cores as it can find. The
PM2 cheatsheet is useful to find other commands.

For feedback on the state of the server

List - $ pm2 list

CPU / Memory Monitoring - $ pm2 monit

View your running app in the browser

View your app from the "Public IP" in any browser

Other useful information

Updating app using Git pull

If you wish to update your app just log on to the server using SSH and use git pull

SSH into server - $ ssh root@212.47.246.30

Move into new app directory - $ cd /app

Git pull latest code - $ git pull

Using a private GitHub repo

There are a few ways to pull a private GitHub repo using SSH keys from a remote server. I use the "Personal access token".
You can generate these tokens in the settings area of GitHub and use git clone with this URL structure:

Saving server setup for later use

You can setup your own images based on your running server for easy deployment by using snapshot and image features of the service.
I did this to create a basic Ubuntu 15 and Node 4 image for this project.

Performance

You will often hear Scaleway C1 servers being compared to the Raspberry Pi 2
in terms of spec, which makes you think it maybe slow. Using all 4 cores they seem perfectly fine for my personal projects. I have yet to find a good set of
benchmarks against other services, which would be useful. This blog is running on scaleway as of (2015-09-30).

Mongodb Issue

Scaleway C1 servers and Mongodb do not play very well together. There is a version you can get to work, but it's not well
supported or considered production level.

Solutions maybe to move db operations onto dedibox by the same
company or use one of the Mongodb hosting services and live with some network latency.

More complex deployments

You can deploy much more complex setups with multiple Node servers, load balancers etc. I am very interested in trying
out the Electric Search. At the moment building all this into images that you can deploy is quite complex for none devops people.

I am still playing with this service but so far its proved interesting and I have moved at least 5 of my small personal sites over.