UNPKG

nodly

Version:

Nodly is an interactive CLI for scaffolding modern Node.js/Express.js backend projects with instant setup for databases, mailers, queues, sockets, and more. Skip boilerplate and start building features fast.

32 lines (29 loc) 1.23 kB
import cors from "cors"; import morgan from "morgan"; import express from "express"; import ApiResponse from "../utils/apiresponses.js"; import { rateLimit } from "express-rate-limit"; export const registerGlobalMiddlewares = (app) => { 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. // store: ... , // Redis, Memcached, etc. See below. }); // Middleware app.use(limiter); app.use(cors({ origin: "*", methods: ["GET", "POST", "PUT", "DELETE"] })); app.use(express.urlencoded({ extended: true })); app.use(express.json({ limit: "50mb" })); app.use(morgan("dev")); // Error Handling Middleware (always put at the end before listen) app.use((err, req, res, next) => { console.error("Unhandled Error:", err.stack); return ApiResponse.error(res, "Internal Server Error"); }); // Testing Route app.get("/test", async (req, res) => { return ApiResponse.success(res, "Nodly App is running!"); }); };