Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Top 3 Best Packages Of Node.js that you should try being a Node.js Developer

  • Last Updated : 12 Feb, 2021

Node.js is an open-source and server-side platform built on Google Chrome’s JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. 

The 3 Best Packages of Node.js that you should try as a developer are:

  1. Chalk Module
  2. Morgan Module
  3. Express Module

Chalk Module: Chalk is used to style the output in your terminal. As a developer, most of our time goes into looking at the terminal to view the success and error messages being logged in the console to make the debugging of our code easier but looking at the terminals plain text most of the time a developer gets bored but if we format the color based upon the success and failure messages then it would make our life easier as a developer. Node.js introduces a package called Chalk which helps us to perform the solution to the problem mentioned above.

Module Installation: You can download the chalk module using this link or install this module using the following command:

npm install chalk

After installing the chalk module, you can require it in your file using the following code:



const chalk = require('chalk');

Filename: index.js

Javascript




// Requiring the module
const chalk = require('chalk');
  
// It is used style a string
console.log(chalk.red('Geeks For Geeks'));
  
// It is used to combine styled and normal strings
console.log(chalk.blue('Geeks') + 'For' + chalk.red('Geeks!'));
  
// Compose multiple styles using the chainable API
console.log(chalk.blue.bgRed.bold('Geeks For Geeks!'));
  
// It is used pass in multiple arguments
console.log(chalk.blue('Geeks', 'For', 'Geeks!'));
  
// It is used to nest the styles
console.log(chalk.red('Geeks'
    chalk.underline.bgBlue('For') + 'Geeks'));

Run the index.js file using the following command:

node index.js

Output:

Chalk Module Demo

 

Morgan Module: Morgan is a great logging tool that anyone works with HTTP servers in node. It generally acts as middleware and allows us to easily log requests, errors, and more to the console. It is named after Dexter Morgan who is a fictional character and the antihero protagonist of the Dexter book series.

 

Module Installation: You can download this module using this link or install this module using the following command:



npm install chalk

After installing the chalk module, you can require it in your file using the following code:

const morgan = require('morgan');

As we know that morgan is a middleware, so we are going to use it with an express server which will make the process easier rather than using the built-in http module in Nodejs.

const express = require('express');
const morgan = require('morgan');
const app = express();

app.listen(5000, () => {
   console.debug('App listening on :5000');
});

To use morgan, we have a suite of presets, which are plug-and-play in morgan. To use morgan, we write morgan(‘tiny’) according to this case, tiny is the name of the predefined format string that we’re using.

 

For using morgan with express we require a predefined formatted string, and we can do the following task by using this code:

 

const app = express();
app.use(morgan(/* This is the  format string */));

The template string which morgan uses is called a format string which is given below:

':method :url :status :res[content-length] - :response-time ms'

Create custom tokens using morgan: It can be achieved using the morgan.token(name, function) function. The first argument which we pass is the name of the token and the second argument is a callback function. Morgan will run each time it logs something using the token. Morgan will pass two parameters to the function i.e req and res. We can create the token which displays the domain that the request was sent through.

morgan.token('host', function(req, res) {
  return req.hostname;
});

Express Module: Express is a lightweight web application framework for node.js used to build the back-end of web applications relatively fast and easily. It provided robust routing, and it focuses on high performance. It has super-high test coverage. It also supports 14+ template engines(Handlebars, EJS, etc). 

Module Installation: You can download the chalk module using this link or install this module using the following command:

npm install express

After installing the express module, you can require it in your file using the following code:

const express = require('express');

Filename: index.js

Javascript




// Requiring the module
var express = require('express'); 
     
// Creating express app object
app = express(); 
    
// Handling /geek Request
app.get('/geek', function(req, res) { 
    res.send('Heyy GeeksforGeeks'); 
}); 
     
// Server setup
app.listen(3000, function() { 
    console.log('Server Listening to port 3000'); 
});

Run the index.js file using the following command:

node index.js

Output:

Server Listening to port 3000



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!