creating custom middleware
creating custom middleware
app.listen(8000, () => {
console.log("App has started and running ");
}); // listen for connection to the app
When the express() function is called, it returns an object, commonly referred
to as app.
This object provides methods for handling HTTP request routing, configuring
middleware, rendering HTML views, and registering template engines.
It receives two arguments: the request object and the response object.
The response object has a send method (res.send()), which is used to send a
response back to the client. In this case, it sends the string "Hello World."
Finally, app.listen() starts the server and listens for incoming connection
Creating Custom Middleware
• Define a function that takes the Request object
as the first argument, the Response object as the
second argument, and a third argument called
next
• The next parameter is a function provided by the
middleware framework, which points to the next
middleware to be executed. Make sure to call
next() before exiting the custom function;
otherwise, the handler will not be invoked.
example
• queryRemover(): Removes the query string
from the URL before passing it to the handler
var express = require('express');
var app = express();
req.url = req.url.split('?')[0]; // This removes the query string from the URL by splitting the string at '?' and
keeping only the part before it.