Posts

Showing posts from August, 2024

Express Production Setup - 5 | Handle Invaild Json

    const express = require (" express ");     const app = express ();     app . use ( express . json ())     // Error handling middleware     app . use (( err , req , res , next ) => {         if ( err instanceof SyntaxError && err . status === 400 && ' body ' in err ) {             res . status ( 400 ). json ({ error: " Invalid JSON " });         } else {             next (); // Passes the error to the next error handler if it's not related to JSON parsing         }     });     app . get (" / ", ( req , res ) => {         res . json ( req . body );     });     app . listen ( 3000 , () => console . log (" server running on port 3000 "));

Express Production Setup - 4 | HELMET

 Helmet helps secure Express apps by setting HTTP response headers. it will remove all header from express app like X-Powered-By : express  we can remove using  express.remove("x-powered-by"); but it's make it easy * also help  XSS attacks index.js      import express from " express ";     import helmet from " helmet ";     const app = express ();     // Use Helmet!     app . use ( helmet ());     app . get (" / ", ( req , res ) => {     res . send (" Hello world! ");     });     app . listen ( 8000 );

Express Production Setup - 3 | CORS

 Express Production Setup - 3 | CORS  To enable Cross-Origin Resource Sharing (CORS) in an Express.js application, you can use the cors middleware package. This middleware allows you to control which domains can access your API, what methods are allowed, and what headers can be sent in requests. index.js     const express = require (' express ');     const cors = require (' cors ');     const app = express ();     // CORS options to allow only POST requests     const corsOptions = {         origin: ' http://example.com ', // Replace with your client's origin         methods: [' POST ', " GET ", " PUT ", " PATCH ", " DELETE "],         credentials: true // allow cookies     };     // Apply CORS middleware with the above options     app . use ( cors ( corsOptions ));     // Example route     app . post (' /a...

Express Production Setup - 2 | Rate Limiting | DDOS

Image
 Simple Rate Limiter In Memory  https://github.com/animir/node-rate-limiter-flexible/wiki  Visit index.js     const express = require (" express ")     const initRateLimiter = require (" ./rate-limiter ")     const app = express ()     // add for all route // app.use(initRateLimiter);     app . get (" / ", ( req , res ) => {         res . send (" hello ")     }) // added only for rate route     app . get (" /rate ", initRateLimiter , ( req , res ) => {         res . send (" ok ")     })     app . listen ( 3000 , () => console . log (" app runnning on port 3000 ")) rate-limiter.js     const { RateLimiterMemory , RateLimiterRedis } = require (' rate-limiter-flexible ');     // Configure the rate limiter     const rateLimiter = new RateLimiterMemory ({       ...

Express Production Setup - 1 | Express Health Checker

index.js const express = require (" express ") const quicker = require (" ./quicker ") const app = express () app . get (" / ", ( req , res ) => {     res . send (" hello ") }) app . get (" /health ", ( req , res ) => {     const HealthData = {         application: quicker . getApplicatonHealth (),         system: quicker . getSystemHealth (),         timestemp: Date . now (),     }     res . json ( HealthData ) }) app . listen ( 3000 , () => console . log (" app runnning on port 3000 ")) quicker.js const os = require (" os ") module . exports = {     getSystemHealth : () => {         return {             cpuUsge: os . loadavg (),             totalMemory: os . totalmem () / 1024 / 1024 + " MB ",             freeMemory: os . freemem ...