6 Useful Node.js Modules Developers Should Know

Using these modules will make your web applications better!

Bryan Dijkhuizen
Nov 25, 2020 · 4 min read
Image for post
Image for post
Photo by Artem Sapegin on Unsplash

Node.js is an open-source and multiplatform JavaScript runtime environment that allows JavaScript to run outside of a browser. Node.js allows developers to use JavaScript for command-line tools and server-side scripting.

Today, we look into the best Node.js modules to make working with Node.js even easier. So here’s a list of 5 useful Node.js modules!

1. Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Image for post
Image for post
Image by Author

Run the following command:

npm install express --save

To create a simple express app follow the code beneath:

const express = require('express')const app = express()app.get('/', function (req, res) {res.send('Hello Medium')})app.listen(3000)

When you run this program, a webserver will run on port 3000 with a default route that displays a message.

2. Bcrypt

A library to help you hash passwords.

Image for post
Image for post

Run the following command:

npm install bcrypt

To hash a password set up the following code:

const bcrypt = require('bcrypt');const saltRounds = 10;const myPlaintextPassword = 'MediumPassword2001';

Now encrypt the password:

bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {// You can use the hashed password over here. (F.E. store it )});

Bcrypt also has a handy function to compare two passwords:

bcrypt.compare(myPlaintextPassword, hash, function(err, result) {// result == true});

3. Mongoose

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.

Image for post
Image for post

Run the following command:

npm install mongoose

First of all, you need to import the module:

const mongoose = require('mongoose');

First, we need to define a connection.

await mongoose.connect('mongodb://localhost/database', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});

Models are made using a Mongoose Schema:

const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;

const Post = new Schema({
author: ObjectId,
title: String,
body: String,
date: Date
});

4. EJS (Embedded JavaScript templates)

EJS simply stands for Embedded JavaScript templates, and we can use it both server-side or client-side.

Image for post
Image for post

Run the following command:

npm install ejs

To use EJS we can use Express to make it easier:

const express = require('express');
const app = express();
app.set('view engine', 'ejs);app.get('/', (req, res) = > {res.render('index');});

This will render the index.ejsfile in a folder called ‘templates’ .

5. PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime, and to facilitate common system admin tasks.

Image for post
Image for post

Run the following command:

npm install pm2 -g

It’s quite easy to use PM2:

pm2 start app.js

This will run your app continuously until you cancel your program.

If you want to show a list of all the apps running:

pm2 list

6. Passport

Passport is Express-compatible authentication middleware for Node.js.

Image for post
Image for post

Run the following command:

npm install passport

Passport uses the concept of strategies to authenticate requests. Strategies can range from verifying username and password credentials, delegated authentication using OAuth.

First, an application needs to be configured:

passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));

To find out more about Passport, visit: NPM — Passport

Conclusion

In this article I hopefully have taught you a couple of new Node.js modules that you can use in your future projects, of course, there are many more handy Node.js modules that you can use but I found these the best at this moment so enjoy exploring these Node.js modules!

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Bryan Dijkhuizen

Written by

Pursuing my Bachelor in Computer Science. Dutch parliament ambitions. Editor of Lessons From History — Evolve You — Political Sense and History in Context.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Bryan Dijkhuizen

Written by

Pursuing my Bachelor in Computer Science. Dutch parliament ambitions. Editor of Lessons From History — Evolve You — Political Sense and History in Context.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store