Skip to content

Instantly share code, notes, and snippets.

@markodayan
Last active July 14, 2020 07:42
Show Gist options
  • Save markodayan/b501623663003b1b5b7a84fd738ff179 to your computer and use it in GitHub Desktop.
Save markodayan/b501623663003b1b5b7a84fd738ff179 to your computer and use it in GitHub Desktop.
Creating Middleware in Express (Important Steps)
// Will create a middleware called logger
// middleware is a called function requiring inputs of (req,res,next) that runs with every http request (if global app middleware)
// next MUST be called to continue on with the http request
const logger = (req,res,next) => {
req.hello = 'Hello World';
console.log('Middleware ran');
next();
};
// because we applied this middleware to the entire express app, it will run with every http request
app.use(logger)
// You can set custom middleware on specific routes (eg auth protection middleware)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment