Skip to content

Instantly share code, notes, and snippets.

@Hero-Development
Created July 20, 2022 16:37
Show Gist options
  • Save Hero-Development/4c73150227d2deb2c125b21e9dd7a698 to your computer and use it in GitHub Desktop.
Save Hero-Development/4c73150227d2deb2c125b21e9dd7a698 to your computer and use it in GitHub Desktop.
Getting started with Express.js; you can use this middleware to log all incoming requests. This helps you verify that your server is running and the data it is receiving.
//app.use() adds a middleware
//middleware tries to handle every request
//this middleware is used for logging
app.use(( req, res, next ) => {
let request = req.method.toUpperCase() +' '+ req.url
if( Object.keys( req.query ).length ){
request += '?'+ querystring.stringify( req.query )
}
if( Object.keys( req.body ).length ){
request += "\r\n"+ JSON.stringify( req.body )
}
console.info( `--------------------------------------------------------------\nRequest: ${request}` )
//next() tells Express to find the next request handler
next()
//NOTES:
// #1 - you can put some logic here, but it should only handle global tasks like authentication and parsing formats
// #2 - you should not put your database logic here
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment