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:
- Chalk Module
- Morgan Module
- 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
15 npm Commands that Every Node.js Developer Should Know NPM stands for Node Package Manager and it is the package manager for the Node JavaScript platform. It put modules in place so that node can find them, and manages dependency conflicts intelligently. Most commonly, it is used to publish, discover, install, and develop node programs. Some Important n
3 min read
What is LTS releases of Node.js why should you care ? Node.js is a powerful and popular open-source runtime environment for JavaScript that allows developers to build scalable, high-performance web applications. One of the key features of Node.js is its ability to receive updates and new versions regularly, which can bring new features, improvements, a
3 min read
The Pros and Cons of Node.JS in Web Development Node.js is a platform that allows developers to build highly scalable applications using JavaScript. It has become the most popular language for developing real-time apps, especially in the enterprise space where it can handle thousands of simultaneous connections while maintaining low latency and h
10 min read
What is package.json in Node.js ? In the world of Node.js development, package.json is a crucial file that serves as the heart of any Node.js project. It acts as a manifest that defines the projectâs metadata, dependencies, scripts, and more. This article will provide an in-depth look at what package.json is, why it's essential, and
4 min read
How to manage the packages in Node.js project ? Node.js is an open-source, cross-platform, back-end JavaScript runtime environment built on the V8 engine and executes the JavaScript code outside the browser. When working on a project in Node.js, you may write code to achieve a specific functionality or to solve a particular problem. There are som
6 min read