6 Useful Node.js Modules Developers Should Know.

Share on facebook
Share on google
Share on twitter
Share on linkedin
Using these modules will make your web applications better!

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 by Author

Installing the module

Run the following command:

npm install express --save

How to use

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.

Installing the module

Run the following command:

npm install bcrypt

How to use

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 )
});

Compare

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.

Installing the module

Run the following command:

npm install mongoose

How to use

First of all, you need to import the module:

const mongoose = require('mongoose');

Connect to MongoDB

First, we need to define a connection.

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

Defining a Model

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.

Installing the module

Run the following command:

npm install ejs

How to use

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.

Installing the module

Run the following command:

npm install pm2 -g

How to use

It’s quite easy to use PM2:

pm2 start app.js

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

List of Apps running

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.

Installing the module

Run the following command:

npm install passport

How to use

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!

bryan@dijkhuizenmedia.com

bryan@dijkhuizenmedia.com

Leave a Replay

Sign up for our Newsletter

Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit