In this 2 part blog post I will explain how to get started with building a RESTful API with Node.js, Express and ECMAScript 6 using a module approach.
Go to https://github.com/LuukMoret/blog-node-api-express-es6-part1 to see the finished code.
Node.js
Node.js is an asynchronous event driven JavaScript runtime, designed to build scalable network applications. Node.js can handle many connections concurrently in contrast to today’s more common concurrency model where OS threads are employed.
Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node are free from worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.
You can read more about Blocking vs Non-Blocking here.
Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. There are alternatives like Koa and Hapi but Express is definitely the most widely used web framework for node.js today.
ECMAScript 6
ECMAScript is a subset of JavaScript. JavaScript is basically ECMAScript at it’s core but builds upon it. Languages such as ActionScript, JavaScript, JScript all use ECMAScript as its core.
ECMAScript 6, also known as ECMAScript 2015, is the latest version of the ECMAScript standard. ES6 is a significant update to the language, and the first update to the language since ES5 was standardized in 2009. Read more about these new features here.
There is also a fantastic JavaScript Style Guide from Airbnb. If you want to enforce these rules in your project, consider using the airbnb eslint npm package found here .
With the above technologies explained we can start writing our node api!
Prerequisites
Project setup
Start by creating a folder and cd into it:
mkdir mynodeapi && cd mynodeapi
Install the official express generator:
npm install express-generator -g
Run the express generator:
express -e --git
-e
defaults to ejs engine instead of jade as (usually) won’t be needing this with an api. We will remove these files later.
--git
will add a .gitignore file.
Install the necessary npm packages:
npm install
Create middleware folder and app folder:
mkdir middleware app
Delete routes folder, views and public folders:
rm -rf routes views public
Remove unused dependencies:
npm uninstall --save ejs
Install runtime dependencies
npm install --save compression cors helmet lodash longjohn nconf request request-debug request-promise winston
Short explanation of each dependency:
- compression - adds gzip compression.
- cors - adds cors support.
- helmet - Helmet helps you secure your Express apps by setting various HTTP headers. It’s not a silver bullet, but it can help!
- lodash - Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
- nconf - Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
- longjohn - Long stack traces for node.js with configurable call trace length.
- request-debug - an easy way to monitor HTTP(S) requests performed by the request module, and their responses from external servers.
- request-promise - adds a Bluebird-powered .then(…) method to Request call objects.
- winston - a simple and universal logging library with support for multiple transports.
Install compile time dependencies
npm install --save-dev chai eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard eslint-watch istanbul mocha nock nodemon sinon sinon-as-promised supertest
Short explanation of each dependency:
- chai - BDD/TDD assertion library for node.js and the browser. Test framework agnostic.
- eslint - a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code.
- eslint-config-standard - module needed for eslint-watch
- eslint-plugin-promise - module needed for eslint-watch
- eslint-plugin-standard - module needed for eslint-watch
- eslint-watch - a simple command line tool that wraps Eslint. Eslint Watch provides file watching and command line improvements to the currently existing Eslint command line interface.
- istanbul - JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests.
- mocha - simple, flexible, test framework
- nock - an HTTP mocking and expectations library for Node.js
- nodemon - nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically estart your node application.
- sinon - standalone and test framework agnostic JavaScript test spies, stubs and mocks.
- sinon - as-promised -sinon with promises
- supertest - the motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the lower-level API provided by super-agent.
Configure ESLint
ESLint is a pluggable linting utility for JavaScript. We can use this to enforce code styling rules.
Create the following files in the project directory to configure ESLint:.eslintignore
with the following content:
|
.eslintrc.yml
with the following content:
You can adjust these options as needed.
Add NPM Tasks
Update the scripts
section with the following npm tasks in the package.json
file:
- start - start the application and watch for file changes
- test - kicks off build process
- build - run lint, tests and coverage
- lint - run lint
- lint:watch - run lint continuously and watch for file changes
- test:single - run tests
- unit-tests - run unit tests
- unit-tests:watch - run unit-tests continuously and watch for file changes
- integration-tests - run integration tests
- coverage - run coverage for all tests
- coverage-unit-tests - run coverage for unit tests
- coverage-integration-tests - run coverage for integration tests
|
npm start
and npm test
can be run as is, the other tasks need to include the run
keyword.npm run build
for example.
Create additional files
Create config.json
to store configurable properties with the following content:
|
Create middleware/configs.js
to bootstrap nconf with the following content:
|
Create middleware/handlers.js
to configure default http handlers with the following content:
|
Create middleware/routes.js
, this will contain the routes of each module. Add the following content:
|
Create middleware/utils.js
with the following content:
|
Update the app.js file, which will bootstrap the middleware components. Replace the entire file with the following content:
|
Update the bin/www file, the entry point of the application. Replace the entire file with the following content:
|
Run npm start
to start the application. You should be able to navigate to http://localhost:3100/ now (which will serve an empty page).
That’s it! The project setup is now done! We will discuss writing modules in part 2.