How to Build a REST API in NodeJS

Reading time ~4 minutes

Building a REST API in NodeJS is simple with a framework specifically made for the task. Eran Hammer and the team at Walmart Labs created HapiJS to connect their mobile platforms with a legacy Java backend. Sounds like complex integrations coupled with with large scale maintainability requirements to me!

I use Hapi to build my platform APIs for that very reason. I’ll demonstrate how simple it is to construct a good foundaton for your REST API.

Today let’s focus on the first step of creating a server. We’ll go over the basics and touch on some handy features.

Installing Hapi

First make sure you have a recent version of NodeJS installed. To get started let’s create a new Node project with npm init`. Just hit enter to accept all the defaults if you want.

npm init
npm install --save hapi nodemon
touch server.js

The --save flag will write the module dependencies to your package.json file. For local development I use nodemon to run my API. It watches the project folder for changes and restarts automatically.

Creating a server

Open the server.js file and type in the following code:

File: server.js
 1 var port = process.env.PORT || 9000;
 2 
 3 var Hapi = require('hapi');
 4 
 5 var server = new Hapi.Server();
 6 
 7 server.connection({ port: port });
 8 
 9 server.start(function () {
10     console.log('Server started at port %d', port);
11     console.log('Resistance is futile');
12 });

No ceremony here! If you want you can provide a port number to start your API at. Otherwise it will default to port 9000.

PORT=9001 nodemon server.js

Application settings

Real world projects usually have some kind of configuration file with environment specific values.

File: config.js
module.exports = {
    "development": {
        "someProvider": "http://dev.some.provider.com"
    },
    "qa": {
        "someProvider": "http://qa.some.provider.com"
    }
};

Instead of having require('./config.js') wherever you need configuration values, you can DRY up your code by giving your configuration values to the server when you call the constructor function.

File: server.js
 1 var port = process.env.PORT || 9000;
 2 var env = process.env.NODE_ENV || 'development';
 3 
 4 var Hapi = require('hapi');
 5 
 6 var server = new Hapi.Server({ app: require('./config')[env] });
 7 
 8 server.connection({ port: port });
 9 
10 server.start(function () {
11     console.log('app setings', server.app);
12 });

Multiple servers

Sometimes it’s useful to partition your app into different servers. Maybe you want the admin part of your API to be completely separate from your core services. A lot of times I need to run a proxy to legacy APIs too.

File: server.js
 1 var port = process.env.PORT || 9000;
 2 var env = process.env.NODE_ENV || 'development';
 3 
 4 var Hapi = require('hapi');
 5 
 6 var server = new Hapi.Server({ app: require('./config')[env] });
 7 
 8 server.connection({ port: 9000, labels: ['core'] });
 9 
10 server.connection({ port: 9001, labels: ['admin'] });
11 
12 server.connection({ port: 9002, labels: ['proxy'] });
13 
14 server.start(function () {
15     console.log('core', server.select('core').info);
16     console.log('admin', server.select('admin').info);
17     console.log('proxy', server.select('proxy').info);
18 });

That’s a wrap on some Hapi server methods I find useful. Next time we’ll learn how to configure routes and add request handlers.

Expanding sliders with Kendo UI.

I've starting hitting walls with Kendo UI not supporting some expected functionality. The Angular UISlider makes it easy change the confi...… Continue reading

Up and Running with ReactJS

Published on June 10, 2015

Freelancing Inspiration

Published on June 09, 2015