Creating your own ReSTful API using node

Now once you have learned about Restful API is, Let’s dive in and create a useful starter restful API using Node and express.

Instead of going step by step approach, I think its easier to explain with full project in hand and explain, Why and How I created the project.Before moving ahead make sure you have downloaded and installed Node.

Now everything ready to start, Let’s clone the node_starter_kit repo

> git clone
> cd node_starter_kit

This is a simple Restful API service using Node & express with following features.

-EcmaScript 2015 / ES6
-Linting with eslint
-Swagger implemented for API documentation
-Unit Testing with Mocha
-Secured with Helmet middleware
-Log requests
-Process Manager2


if you open the project in any editor of your choice, you should be able to see following folder structure.

Folder Structure

-dist (Contains minified compiled files)
-public (contains external folder api-docs, which is copied from Swagger for documentation)
-src (contains all the main source codes)
-test (tests for unit testing using Mocha)
-.babelrc (config file for babel)
-.eslintrc (config file for eslint for linting)
-.gitignore ( you probably know this one already) ( you probably know this one already)
-pacakge.json (contains all the informations about the project, dependencies and run scripts )

If you see script in the package.json file, we can start the dev server by typing

>npm run dev (> nodemon src/api.js — exec babel-node src/api.js)

Let’s move in to our main starting script page, i.e src/api.js

import ‘babel-polyfill’;
//without babel-polyfill, babel only allows you to use features like arrow functions, destructing, default arguments, and other syntax-specific features introduced in ES6.

import cors from ‘cors’;
//CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.

import path from ‘path’;
//The path module provides utilities for working with file and directory paths.

import helmet from ‘helmet’;
// Helmet, is a set of express middleware, to help secure from cross-site scripting(XSS),script injection, clickjacking,insecure requests, etc.

import morgan from ‘morgan’;
//HTTP request logger middleware for node.js

import express from ‘express’;
//Web framework for node, crates server, routes, etc

import bodyParser from ‘body-parser’;
//Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

We use all the above mentioned middleware using app.use()
app.use(express.static(path.join(__dirname, ‘/../public’)));
app.use(‘/api’, routes);

Starting server with 
const app = express();
app.listen(APP_PORT, () =>{
 logger.log(‘info’, ‘Server started at ‘ + APP_HOST + ‘:’ + APP_PORT);

so generally it should be available at localhost:3000

Defining Routes:

import routes from ‘./routes’;
app.use(‘/api’, routes);


Defining route is as simple as this,

router.get(‘/students, studentController().getStudentList);‘/students, studentController().addStudent);



Test for the service.