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.
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
--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
Creating a server
Open the server.js file and type in the following code:
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
Real world projects usually have some kind of configuration file with environment specific values.
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.
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.
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.