13 useful Modules for any NodeJS RESTful API Boilerplate
JavaScript and Node.js
As of the beginning of 2015, JavaScript stands as the first most popular language in GitHub and the second in terms of questions tagged on Stack Overflow, next to only Java, with around a million questions tagged on it. The evolvement of this language from a language to add enhancements and widgets to a web page into its own fully-fledged ecosystem was the main reason for this popularity.
With the introduction of Node.js, JavaScript has officially gone in a direction that was never even possible before. Now you can use JavaScript on the server and you can also use it to develop full-scale enterprise-level applications.
Node.js is a runtime environment for JavaScript applications built on the top of Google’s V8 engine, and it can be installed on most of the major operating systems including Windows. The V8 engine which is an open source high performance engine written in C++ offers an efficient model, where the JavaScript code will be compiled into machine-level code and the executions will happen on the compiled code instead of interpreting the code.
Node.js has an event-driven architecture capable of asynchronous I/O. This architecture aims to optimize throughput and scalability in Web applications with many input/output operations, as well as for real-time Web applications (e.g., real-time communication programs and browser games).
It was created because concurrency is difficult in many server-side programming languages, and often leads to poor performance. Developers can create highly scalable servers without using threading, by using a simplified model of event-driven programming that uses asynchronous callbacks to signal the completion of a task.
In the next section, I will introduce some useful Node.js modules that can be integrated in any boilerplate in order to develop robust NodeJS based RESTful APIs.
Boilerplate Modules for Node.js
The module system in Node.js is so powerful that consuming a third-party module written by other developers is a piece of cake. It has a simple module loading system where files and modules are in one-to-one correspondence.
Node.js includes its own package manager called npm, which is a registry that currently contains over 60,000 unique modules written in JavaScript. These modules are completely open source and available to you via a few short commands. In addition, you can release your own personal modules via npm and allow other developers to use them.
If you are planning on developing a Node.js API, I would highly recommend you these modules:
Express
Express.js is a web framework that adds a thin layer over Node.js to provide a robust set of features such as HTTP utility, routing table, middlewares setup, HTML rendering, serving static files, etc. It is probably the most used module by Node developers.
There are some modules built on top of Express to extend its functionality like express-session to handle server side user sessions, cookie-parser to manage cookies, and hogan-express as templating engine to handle HTML views.
Mongoose
Mongoose.js is an object modeling layer that allows you to connect and to perform CRUD operations on non-relational data stored in MongoDB. It is easy to use, and it has a great documentation.
Mongoose provides a straight-forward, schema-based solution with built-in type casting, validation, query building, business logic hooks and more, out of the box.
Passport
Passport.js is a Node.js middleware for authentication that can be used by any Express-based web application. It provides very flexible and modular authentication mechanism based on strategies. These strategies allow developers to select the authentication method (username/password, Facebook token, twitter token, OAuth, and many more).
The Passport module requires session management on the server to store and retrieve users sessions.
JSON Web Token
The JWT Node.js module implements industry standard RFC 7519 for defining a compact and self-contained way for securely transmitting information between parties as a JSON object. It is very easy to use, usually by just including encoded authentication string which can be digitally signed in the HTTP header.
URL
URL is a build in Node.js module that provides URL parsing functionalities. Developers use this module to get multiple meaningful components from a specified URL such as protocol, host, path, etc.
Body-Parser
Body-Parser is an Express based module for parsing incoming requests bodies. It populates the parsed body into the req.body property which you can use to handle the request data. It returns an empty object ({}
) if there was no body to parse.
This module introduces many parsers depending on your data type like JSON body parser, raw body parser, URL-encoded form body parser, etc.
Formidable
The Node-Formidable module is for parsing form data, especially file uploads. Node.js by itself does not provide any way to handle the work required to process the data of the form (like validations). It just provides raw body data. And here comes the benefit of this module which can help you parse form data and use it in your application.
JSON Schema
JSON Schema validator module, as the name suggest, helps you validate json object like in request body against a specific schema. The main feature of this module is that you may add your own custom format functions for validating input and returning a boolean value. The instance passes validation if the function returns nothing. A descriptive validation error is produced when the validation fails.
Node Mailer
Node-Mailer module allows you to send e-mails using plain SMTP or specific type of transporter plugin such as SendGrid and Amazon SES. This module is well documented and provides you with simple examples about supported transports.
Socket IO
Socket.io is a real time messaging framework that allows you to emit and receive events like connect, disconnect, message, etc. This module is very useful not only to chat applications but also to applications that require real time response on specific events or requests with the ability to broadcast messages and leverage WebSocket semantics.
Stripe
The Stripe-Node module provides you with ability to conduct online payments via Stripe service provider. It is very easy to use, and I personally recommend it for web-based payments.
In order to use this module you have to register yourself to get your API secret keys. If you want mobile-based payments, I would recommend In-App Purchase where you can use modules like Node-iap to validate and confirm the purchase.
Production Process Manager
The PM2 module is a great choice when you want to implement some kind of continuous integration on your development or production server. PM2 provides you with a built-in load balancer for your Node.js applications, and allows you to keep the applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
It also monitors your applications and logs status changes, notifications, and standard output messages.
Node Schedule
The Node-Schedule module allows you to schedule jobs for execution at specific dates, with optional recurrence rules in cron-like manner. It is designed for in-process scheduling, i.e. scheduled jobs will only fire as long as your script is running, and the schedule will disappear when execution completes.
Found your favorite NodeJS Modules?
… if not, it isn’t a big deal because there are lots and lots of other great modules out there for you to explore. For example you could start at the official npm website.