Last active
July 14, 2020 07:42
-
-
Save markodayan/b501623663003b1b5b7a84fd738ff179 to your computer and use it in GitHub Desktop.
Creating Middleware in Express (Important Steps)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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