How can we help?

Help via Chat from monday to friday

Node.JS at Umbler

Welcome to Umbler's Node.JS HUB

Here you'll find all tutorials about Node.JS at Umbler. From setup to deploy, check out the main steps to host your Node.JS website/app.

If you want to know more about our Node.JS hosting platform? Click here.
You may also want to check out our MongoDB guide, click here.

Precautions with package.json

In order to deploy your Node.JS application, you must take some precautions with a file called package.json.

You need to make sure it has the start script on the scripts part, as you can see in the package.json example below.

{
  "name": "umbler-node",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "check-node-version": "^2.1.0",
    "cluster": "^0.7.7",
    "express": "^4.15.2",
    "os": "^0.1.1"
  }
}

In the example, the "start":"node app" or "start":"your starting file" must exist so your application can run properly. 

Our environment uses a Linux platform, so it's important to remember that if your application was originally developed on a Windows environment, it might be using packages with case-sensitive names, for example: "Express" : "^4.15.2". Windows ignores case-sensitive, and looks for packages on NPM without upper case letters. Linux by default uses case-sensitive and doesn't ignore it, so if you are deploying and have dependencies with upper case letters, it won't work. We recommend you to change all dependencies name's to lower case.

How to deploy your application?

The first step is to access Umbler's Control Panel > Your Domain > Site > Deploy > and copy your Git URL:

1-Node-JS.PNG

Using the HTTPS URL, you must authenticate with user and password from your Umbler panel each time you perform a new deploy. With the SSH URL you can deploy without having to authenticate with username and password. Just add the public key of your computer inside the Umbler panel, in the SSH Keys option. If you haven't generated the public key yet, this guide shows in detail how to do so ;)

 

After getting your Git URL, you should start a Git repository in your application folder if you haven't already. You can then push a commit into this repository. Here is an example of instructions for starting the repository, committing and pushing it to the Umbler server.

If you deploy with a yarn.lock file, the server is going to identify the file and instead of executing NPM INSTALL, it will execute YARN INSTALL.

git init
git add .
git commit -m "My node.JS deploy at Umbler"
git remote add umbler YOUR_GIT_URL
git push umbler master

 

The first command used is git init, responsible for initializing your Git repository. You can get more information about Git here: Accessing Git SCM.

Once the push is executed to Umbler's server, we'll check if among your files there's a node_modules directory. That's where the dependencies of your application are kept. If you deploy your application with node_modules, NPM INSTALL won't be executed. If you deploy your application without node_modules (that's usually what people do), NPM INSTALL will be executed and all the dependencies from your package.json will be downloaded.

If you are pushing to Umbler for the first time, you'll have to identify/authenticate. The authentication is the same used to access Umbler's Control Panel: e-mail and password. In case you have logged in using a third party service, such as Facebook or Google, you'll need to reset your password so you can successfuly authenticate.

 

When the deploy is finished, Git will return a message similar to this one:

remote: Build OK
remote: Umbler: Build completed successfully!  Your application will be updated and published soon.
remote: Umbler: Temp address: http://node-na-umbler-com.umbler.net
To https://tatooine.deploy.umbler.com/7e8vlyok/node-at-umbler-com.git

 

It's important to remember that your application must be running on port 3000, or using an environment variable PORT set to this value, so your application can be accessed externally.

Here's an example:

var express = require('express');
var app = express();

//... your code here ...
                                
var port = process.env.PORT || 3000;
app.listen(port, function () {
    console.log('Umbler listening on port %s', port);
});
 

Now that you have deployed and your application is being published, you just need to wait the server to start!  

Verifying your deploy logs

 You can verify your deploys logs going to Umbler's Control Panel > Deploy and clicking on see logs, as shown in the image below:

2-Node-JS.PNG

Clicking on see logs you'll be able to see your deploy logs:

3-Node-JS-deploy-logs.PNG

Verifying your application logs

In order to verify your application logs, you need to enable them first.

An important detail is that your application logs stay available for 24 hours only. You can enable them accessing Umbler's Control Panel > Your domain > Logs, and clicking on Enable for 24h as shown in the image below:

4-Node-JS-app-logs.png

 Once enabled, it will look like this:

5-Node-JS-app-logs.png

Node.JS environment variables

On Umbler's Control Panel, you can define environment variables that your application can use. By default our system has already a few predefined variables (PORT, TZ, LANG, and LANGUAGE) that cannot be changed.

You can access the environment variables going on Umbler's Control Panel > Website > Settings > Node.JS Environment Variables

You'll see a similar page:

6-Node-JS-env.png

Now you can see all your environment variables. By default the PORT variable is created, as our Node.JS runs on port 3000, we have already created this PORT environment variable in order to help you :)

 To create a new environment variable just click on Create environment variable (marked with the red square) and use it in your application through the following code:

7-Node-JS-env.png

Here's an example of how to call the environment variable in your application

process.env.VARIABLE_NAME

Scaling on Node.JS

In Umbler you can scale your application both horizontally andvertically, you can increase the size of your container and also increase the number of processes that suit your application.

It's simple to upgrade your plan by going to the Control Panel > Site > And then clicking on the button marked in orange, followed by the button in red.

8-Node-JS-resize.png

Now you will be on this screen:

resize.png

Choose how you want to scale your application, how many processes you want Node.JS to have available to use, and how large they must be.

Remember that if you use sessions in your application, you need to store it somewhere else like a database or save the token in the user's browser as a Cookie. If you store the session token in the process, when Node.JS answer to another request, your current session might be overwritten.

Node.JS errors

The Error 503 indicates that your application is not available. This error can occur due to deploy failure or some internal error that occurs in your container.

We recommend that you check the error log and also make sure your Package.json is configured according to Umbler's recommendations.

The Error 500 is normally related to problems in your application.

To solve it, we recommend you to check your deploy logs and see what error ocurred.

The Error 404 indicates that some request to your application was not found.

Normally this error is informed in cases where an URL (from images, pages , etc) informed by your code is not on the specified location.

To solve this error, we recommend that you run the application locally and check if the error persists. If so, just check where the request is really located, so you can take the correct path and update your application.

The Error 403 means that the final user doesn't have permission to make the request.

It usually happens when you have some kind of authentication in your application, and a user that is not authenticated tries to make something he is not allowed to do.

Transpilers

To run Babel you need to configure a parameter called "postinstall": "babel-node index.js" inside your package.json. Since the files that Babel writes must be compiled, you need to configure that parameter. Check out bellow an example of a package.json that is configured correctly.

{
  "name": "babelandnode",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index",
    "postinstall": "babel-node index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-preset-latest": "^6.24.1"
  }
}

To run TypeScript you need to configure a parameter on your package.json: "postinstall": "grunt".

This configuration is made so the TypeScript is compiled before it's executed by Node.JS. Check out bellow an example of a package.json that is configured correctly.

{
  "name": "heros",
  "description": "The tour of heros",
  "version": "1.0.0",
  "private": true,
  "author": "Brian Love",
  "scripts": {
    "dev": "NODE_ENV=development nodemon ./bin/www",
    "grunt": "grunt",
    "start": "node ./bin/www",
    "postinstall": "grunt"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "cookie-parser": "^1.4.3",
    "errorhandler": "^1.4.3",
    "express": "^4.14.0",
    "method-override": "^2.3.6",
    "morgan": "^1.7.0",
    "pug": "^2.0.0-beta6"
  },
  "devDependencies": {
    "@types/body-parser": "0.0.33",
    "@types/cookie-parser": "^1.3.30",
    "@types/errorhandler": "0.0.30",
    "@types/method-override": "0.0.29",
    "@types/morgan": "^1.7.32",
    "grunt": "^1.0.1",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-ts": "^6.0.0-beta.3",
    "nodemon": "^1.11.0",
    "typescript": "^2.0.8"
  }
}

To run Webpack you need to configure a parameter on your package.json. Check out bellow an example of a package.json that is configured correctly.

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --inline --hot --port 3000",
    "build": "webpack -p --port 3000"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.21.0",
    "babel-loader": "^6.2.10",
    "babel-preset-es2015": "^6.18.0",
    "css-loader": "^0.26.1",
    "extract-text-webpack-plugin": "^2.0.0-beta.4",
    "file-loader": "^0.9.0",
    "node-sass": "^4.1.0",
    "sass-loader": "^4.1.0",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.7",
    "webpack": "^2.1.0-beta.28",
    "webpack-dev-server": "^2.2.0-rc.0"
  },
  "dependencies": {
    "d3": "^4.4.0",
    "lodash": "^4.17.2"
  }
}

The parameter you need is "start": "webpack-dev-server --inline --hot --port 3000". This configuration is required so Webpack can run on port 3000.

Ghost at Umbler

In order to run Ghost with Node.JS you need to change some configurations at you config.js file.

Make sure you application is available on port 3000 and also that your host is configure to 0.0.0.0, as the following example:

var path = require('path'),
    config;

config = {
    production: {
        url: 'http://localhost:3000',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            port: '3000',
            host: '0.0.0.0'
        }
    }
    ...

Configuring New Relic

The first step is to set up on the website New Relic and then create an application in Node.JS there, so you'll receive a key to continue with your configuration.

To set up New Relic on your application, you need to install the New Relic package, simply open your Git Bash or any terminal and run the following command:

npm install newrelic --save

After installing the module, your will access the node_modules folder > newrelic and then copy the newrelic.js file to the root folder of your application.

After moving it to the root folder, open the newrelic.js file on a text editor and place your key and the name of your application.

Here is an example of a file that is correctly configured:

'use strict'
exports.config = {
  app_name: ['Test Umbler'],

  license_key: '94f417a76a019a3de38285a589c97631ec955eb5',
  logging: {
    level: 'info'
  }
}

After configuring newrelic.js file, you just need to add the require('newrelic') at the beginning of your server file (the one that starts Node.JS, usually called index.js or app.js). Here's an example of the file:

require('newrelic')
const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('test')
})

app.listen(3000, () => {
    console.log('running at 3000')
})

A few minutes after your application starts being accessed, you'll be able to see logs of your application on New Relic's Control Panel.

Bower

To use Bower you need to send your node_modules folder when you do a git push.

A way of doing that is renaming a file called .gitignore (if you use it your application) to something else and then pushing again. It's worth remembering that you need to run npm install in your local computed before pushing your application along with node_modules folder.

Nodemon

You don't need to use Nodemon, because when your container goes into production, any error that occurs with your application will restart the container automatically. Also, for security reasons we do not allow the installation of Nodemon, because to install it, run the npm install nodemon -g command, and your are not allowed to install global dependenciess.

To upload your server with us, simply configure your package.json with the start command.

Redirecting to HTTPS on Node.JS

There are several ways to analyze the protocol of a request in Node.js, but the vast majority of them are not compatible with architectures that use load balancers. The load balancer acts as an intermediary between the client request and the web server, and it's the responsible for opening the connection to the server. Normally the web server can only know the protocol used by the load balancer request, but the important protocol to know when redirecting to HTTPS is used by the client in relation to the load balancer.

Since this information is fundamental, Umbler sets up its load balancers to store the request protocol in the X-Forwarded-Proto header. In your code it is possible to retrieve this information contained in the header, and if the value is different from "https", redirect to a URL that uses the HTTPS protocol, otherwise, follow the normal flow of the application. To make it clear, Here's an example using the Express framework.

app.get('*', (req, res, next) => {
    if (req.headers['x-forwarded-proto'] != 'https') {
        // check if the header returns HTTP or HTTPS
        res.redirect("https://" + req.headers.host + req.url);
        // redirects to HTTPS
    } else {
        next();
        // continues executing the file
    }
});
Remember you can enable SSL on your application by using the Let's Encrypt addon at Umbler. Click here and learn how to enable it.

 If you have any doubts, just call us at the bottom right corner. We are here to help!