📚 Tutorial🕸️ Express JsAdvance - Plugins

Plugins

1. cors

Providing a Connect/Express middleware that can be used to enable CORS with various options. https://www.npmjs.com/package/cors

yarn add cors
server.ts
import express, { Request, Response } from "express";
import cors from "cors";
 
const app = express();
 
// Add This
app.use(cors())
 
app.get("/", function (req: Request, res: Response) {
    res.end("Hello mom!");
});
 
const PORT = 8181;
app.listen(PORT, () => {
  console.log(`Listening at http://localhost:${PORT}/`);
});

2. express-rate-limit

Basic rate-limiting middleware for Express. https://www.npmjs.com/package/express-rate-limit

yarn add express-rate-limit
server.ts
import express, { Request, Response } from "express";
import { rateLimit } from 'express-rate-limit'
 
const app = express();
 
// Add This
const limiter = rateLimit({
	windowMs: 15 * 60 * 1000, // 15 minutes
	limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
	standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
	legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
})
app.use(limiter)
 
app.get("/", function (req: Request, res: Response) {
    res.end("Hello mom!");
});
 
const PORT = 8181;
app.listen(PORT, () => {
  console.log(`Listening at http://localhost:${PORT}/`);
});

3. helmet

Help secure Express apps by setting HTTP response headers. https://www.npmjs.com/package/helmet

yarn add helmet
server.ts
import express, { Request, Response } from "express";
import helmet from "helmet";
 
const app = express();
 
// Add This
app.use(helmet());
 
app.get("/", function (req: Request, res: Response) {
    res.end("Hello mom!");
});
 
const PORT = 8181;
app.listen(PORT, () => {
  console.log(`Listening at http://localhost:${PORT}/`);
});