Build an Express middleware to log incoming HTTP requests to the server.
Build an Express middleware to log incoming HTTP requests to the server.
16526-Jul-2023
Updated on 27-Jul-2023
Home / DeveloperSection / Forums / Build an Express middleware to log incoming HTTP requests to the server.
Build an Express middleware to log incoming HTTP requests to the server.
Aryan Kumar
27-Jul-2023Sure, here is the code for an Express middleware to log incoming HTTP requests to the server:
JavaScript
This code first imports the
express
module. Then, it defines a middleware function calledlogger
. Thelogger
function takes three arguments: the request object, the response object, and the next middleware function.The
logger
function logs the following information about the request:The
logger
function then calls thenext()
function to pass control to the next middleware function in the stack.Finally, the app creates a route that listens on the
/
path. This route uses thelogger
middleware to log incoming requests.To run this code, you can save it as a file called
app.js
and then run it from the command line:This will start the Express server on port 3000. You can then make requests to the server and the
logger
middleware will log the requests to the console.