| Node.js provides a wide variety of external modules that developers can use to add functionality to their applications. These modules are usually available via npm (Node Package Manager). Here are some commonly used external Node.js modules: 1. Express - Description: A fast, unopinionated, minimalist web framework for Node.js. It simplifies routing, middleware management, and handling HTTP requests and responses.
- Installation:
npm install express Usage: const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
2. Mongoose - Description: An elegant MongoDB object modeling tool designed to work in an asynchronous environment.
- Installation:
npm install mongoose Usage: const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const Schema = mongoose.Schema;
const MyModel = mongoose.model('MyModel', new Schema({ name: String }));
const doc = new MyModel({ name: 'Example' });
doc.save();
3. Lodash - Description: A utility library that provides convenience functions for common programming tasks, such as manipulating arrays and objects.
- Installation:
npm install lodash Usage: const _ = require('lodash');
const numbers = [1, 2, 3, 4, 5];
const evens = _.filter(numbers, n => n % 2 === 0);
console.log(evens); // [2, 4]
4. Axios 5. jsonwebtoken - Description: A module to sign and verify JSON Web Tokens (JWT), used for secure authentication in web applications.
- Installation:
npm install jsonwebtoken Usage: const jwt = require('jsonwebtoken');
const token = jwt.sign({ userId: 123 }, 'secretkey', { expiresIn: '1h' });
jwt.verify(token, 'secretkey', (err, decoded) => {
if (err) {
console.error('Token verification failed:', err);
} else {
console.log('Decoded token:', decoded);
}
});
6. nodemail - Description: A module to send emails easily.
- Installation:
npm install nodemailer Usage: const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword',
},
});
const mailOptions = {
from: 'youremail@gmail.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world',
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending email:', error);
} else {
console.log('Email sent:', info.response);
}
});
7. bcrypt - Description: A module to hash passwords and compare them, often used for secure password storage.
- Installation:
npm install bcrypt Usage: const bcrypt = require('bcrypt');
const saltRounds = 10;
bcrypt.hash('myPassword', saltRounds, (err, hash) => {
if (err) {
console.error('Error hashing password:', err);
} else {
console.log('Hashed password:', hash);
}
});
These are just a few examples of the many external modules available in Node.js. Depending on your project's needs, there are countless others that can be utilized to streamline development and add advanced functionality. |