Middleware
is a utility that enables the queuing of request
listeners for an HTTP Server
.
.use
is used to add new request
listeners to the queue. These listeners will be invoked with the following arguments:
Argument. | Type | Description |
---|---|---|
request |
http.IncomingMessage | The HTTP Request. |
response |
http.ServerResponse | The HTTP Response. |
next |
Function |
This function is used to continue to the next listener in the queue. |
.clear
is used to clear all request
listeners from the queue.
const middleware = require('notify-middleware')
const app = middleware()
// Since this listener is first one, it will always be invoked.
app.use((req, res, next) => {
if (req.method === 'POST') next()
})
// This listener is only invoked on a POST HTTP Request.
app.use((req, res, next) => {
console.log('POST REQUEST!!')
})