on-request — take over the request handler of a Node HTTP server.
import http from 'http';
import onRequest from 'on-request';
const srv = http.createServer((req, res) => {
res.end('Handling', req.url);
});
onRequest(srv, (req, res, next) => {
if ('/special' === req.url) {
res.end('Special request!');
} else {
next();
}
});
onRequest(srv : http.Server, fn : Function, onError : fn)
default. Use import onRequest from 'on-request' (ES6) or require('on-request').default (legacy).onError is executed when next is invoked but no other request handlers are present. By default it returns 501 Unimplemented to prevent requests from hanging and allocating resources. It's invoked with the following parameters:
http.IncomingRequesthttp.OutgoingResponsefn function receives three parameters:
http.IncomingRequesthttp.OutgoingResponserequest listeners registered after onRequest will be fired simultaneously.onRequest all the existing listeners will be "removed' and no longer exposed to .listeners().