Open In App

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

Last Updated : 12 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Top JavaScript Playgrounds every developer should try!
Javascript has seen significant advancements in recent years, allowing developers to create, edit, and run code using an online environment called “Playground”. These playgrounds offer several features over the traditional offline code editor that runs on a development environment. Just like in a regular playground for children, a javascript playgr
10 min read
10 Best Free Elementor Themes That You Can Try
To give an amazing look to your website, Elementor Themes are the perfect choice. For WordPress, Elementor is the most popular "drag-and-drop" page builder plugin. Elementor Themes are themes where there's no conflict between the theme and the page builder. It is a powerful tool to create everything from full websites to specialized landing pages.
6 min read
13 Things You Should Know Before You Enter In Web Development
Every beginner starts their journey in web development learning frontend part, backend part, and some tools which are important to build the web project. Building your first project on your own after so many struggles is an exciting feeling for the beginners. You start having a lot of expectations from your job but when you enter the industry you f
7 min read
Top 10 Tools That Every Web Developer Must Try Once
If you want to do any task easily, effectively, and efficiently then you should use some tools. Though there are lots of different software development tools available. You have to select the most useful tools depending on the requirement and technology that you are working on. A good programmer should know which tools are better than others and al
6 min read
Top npm packages for node
NodeJS has become an effective tool for server-side development, thanks to its extensive ecosystem of npm packages. These packages offer a wide range of functionalities, from web frameworks to utility libraries, enabling users to build robust and scalable applications efficiently. In this article, we'll delve into seven top npm packages for NodeJS
4 min read
Where should secret keys should be stored for a Node.js app ?
Securing the sensitive data/credentials like 3rd Party API credentials, Session Secret Key, DB Connection Credentials, Security Token, Encryption/Decryption Keys is extremely important as publicly exposing the credentials can result in your account being compromised, which could lead to unexpected charges on your account. Putting secret values in t
3 min read
What Happens when you try to Add a Duplicate Value to a Set in JavaScript ?
When you try to add a duplicate value to a Set in JavaScript using the add() method, the Set ignores the duplicate value. Sets are designed to store unique values only, and adding a value that is already present in the Set does not result in duplicates. Example: Here, even though the value 1 is added to the Set twice, the Set only contains unique v
1 min read
6 Best CSS frameworks You should Know to design Attractive Websites
If you want to speed up your website and development process, or want to add some classy designs to your website, then you're at the right place. Awesome new popups, speed, ultimate colors and themes are waiting for you. Front end development is the complete pack of creation and colors and have amazing frameworks. Every framework has its own unique
3 min read
Node.js Vs Django: Top Differences That Web Developer Should Know [2024]
There has always been a heated debate over which one to choose from Node.js or Django. In this blog, we will be addressing the same. So before getting directly to comparison let's have a small intro to Nodejs and Django. What is Node.js?Node.js is a javascript runtime environment. The runtime environment provides everything which is required for a
7 min read
Node.js vs Browser - Top Differences That Every Developer Should Know
Node.js and Web browsers are two different but interrelated technologies in web development. JavaScript is executed in both the environment, node.js, and browser but for different use cases. Since JavaScript is the common Programming language in both, it is a huge advantage for developers to code both front-end and backend in the same language. Des
6 min read