List of useful node packages
A Node Web framework
Usage:
npm i express --save
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
A modern JavaScript utility library delivering modularity, performance & extras
Usage:
npm i -g npm
npm i --save lodash
MongoDB object modeling tool designed to work in an asynchronous environment
Usage:
npm i mongoose
Create friendly url names
Add geocodes from address provided
Middleware for handling file uploads
Usage:
npm i express-fileupload
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
Styling node.js console logs
Usage:
npm i colors
const colors = require('colors');
console.log('test'.green.inverse);
Usage:
npm i jsonwebtoken
const jwt = require('jsonwebtoken');
UserSchema.methods.getSignedJwtToken = function() {
return jwt.sign(
{ id: this._id,},
'theosdasdnadaf',
{ expiresIn: 30d }
);
};
Password hashing
Usage:
npm i bcryptjs
const bcrypt = require('bcryptjs');
UserSchema.pre('save', async function(next) {
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
Storing/set tokens on cookies
npm i cookie-parser
// Cookie parser in Server.js file
const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()
app.use(cookieParser())
Sending emails
npm i nodemailer
const nodemailer = require('nodemailer');
const sendEmail = async (options) => {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_EMAIL,
pass: process.env.SMTP_PASSWORD,
},
});
const message = {
from: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
to: options.email,
subject: options.subject,
text: options.message,
};
const info = await transporter.sendMail(message);
console.log('Message sent: %s', info.messageId);
};
module.exports = sendEmail;
Standard node module for cryptographic functionality (Password resets, tokens)
const crypto = require('crypto');
const resetPasswordToken = crypto
.createHash('sha256')
.update(req.params.resettoken)
.digest('hex');
Authentication middleware which support username & password, Facebook and Google
npm install passport
passport.authenticate('facebook')
Most popular chart javascript library for bar charts, scatter charts etc
npm install react-chartjs-2 chart.js
import { Bar } from 'react-chartjs-2';
<Bar
data={data}
width={100}
height={50}
options={{ maintainAspectRatio: false }}
/>
A modern approach to copy text to clipboard
npm install clipboard
Headless Chrome Node.js API which can be used to control Chrome or Chromium
// Downloads recent version of chromium (~170mb)
npm i puppeteer
//Light version which doesn't download chromium
npm i puppeteer-core
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
Check for unused npm dependencies
npm install -g depcheck
depcheck [directory] [arguments]
express-mongo-sanitize (Security)
Sanitizes user-supplied data to prevent MongoDB Operator Injection
npm i express-mongo-sanitize
// Example Vulneralbility
{
"email":{"$gt":""},
"password": "123456"
}
//Usage
mongoSanitize = require('express-mongo-sanitize');
app.use(mongoSanitize());
helmet (Security)
Secure your Express apps by setting various HTTP headers
npm i helmet
//Usage
const helmet = require('helmet')
app.use(helmet())
xss-clean (Security)
Middleware to sanitize user input coming from POST body, GET queries, and url params
npm i xss-clean
//Usage
const xss = require('xss-clean')
app.use(xss())
express-rate-limit (Security)
Basic rate-limiting middleware for Express. Used to limit repeated requests to public APIs
npm i express-rate-limit
//Usage
const rateLimit = require("express-rate-limit");
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
// apply to all requests
app.use(limiter);
hpp (Security)
Middleware to protect against HTTP Parameter Pollution attacks
npm i hpp
//Usage
const hpp = require('hpp');
app.use(hpp());
middleware that can be used to enable CORS with various options
npm i cors
//Usage
const cors = require('cors');
app.use(cors());