Content: Blog

Tips and Tricks

Setup a Node.js + Express app in 60 seconds

Thomas Bailey

April 11, 2018

Did you know we also include ready-to-go Node.js support? here are a few quick steps to getting a Node + Express app set up in less than a minute.

Before you begin, be sure to install the Divio Desktop application.

Start by creating a new project in the usual way from the Divio Control Panel and be sure to select :

  • No Python
  • Blank Project
  • Node.js

This will prepare a Dockerised Node boilerplate project based upon the official Docker image node:6.10.

In the Divio Desktop application, select your project and choose "Setup". This will pull down and sync all the files locally and you can start a server with the initial template application immediately.

If you navigate to your newly created project on your local machine, you will see the familiar index.js and package.json and other files related to Docker configuration.

Note that if you prefer to use a later version of Node.js and are not readily familiar with Docker then simply change the Dockerfile "FROM" line to indicate which Docker Node image should be used. In this example we will the latest Node 9.

FROM node:9

You may need to rebuild your Docker image to reflect the change in Node docker image version.

docker build --no-cache .

Now you can add Express and any other Node modules in the usual way:

npm install express --save

Open the index.js file and replace the contents with the classic Hello World for Express.

const express = require('express')

const app = express()

app.get('/', (req, res) => res.send('Hello Node on Divio!'))

app.listen(80, function () {
  console.log('Listening on port 80')
})

You can verify everything is working as expected by starting your app in the usual way.

npm start

Now start the Docker container by selecting Start from the Divio Desktop app. You can point your browser to localhost:3000 and you should be greeted by "Hello Node on Divio!".

Note that your app is listening on port 80 "inside" the container but this is port-forwarded to 3000 which you can access "outside" the Docker container. When deployed on Divio it will be available on port 80 / 8080 (https) as expected.

Finally to release your Node app to the world, choose the Deploy option on the test server. The Divio Desktop app will upload your project files and Divio will prepare the environment. Once complete, select the eye-icon to preview your freshly prepared public test site.

Now you can add your routing and static files as usual and since its now Dockerised for you, your local development environment will be identical to your test or live version. 

If you need any help in your initial setup with Divio, try the getting started tutorial.