A basic middleware is a function that takes 3 arguments request, response and next.
Then by app.use, a middleware is mounted to the Express App Middlewares Stack. Request and response are manipulated in each middleware then piped to the next one through the call of next().
For example, the below code:
var express = require('express');
var app = express();
app.use((request, response, next) => {
request.propA = "blah blah";
next();
});
app.use('/special-path', (request, response, next) => {
request.propB = request.propA + " blah";
if (request.propB === "blah blah blah")
next();
else
response.end('invalid');
});
app.use((request, response, next) => {
response.end(request.propB);
});
app.listen(1337);
Can roughly be translated to:
var http = require('http');
http.createServer((request, response) => {
//Middleware 1
if (isMatch(request.url, '*')) {
request.propA = "blah blah";
}
//Middleware 2
if (isMatch(request.url, "/special-path")) {
request.propB = request.propA + " blah";
if (request.propB !== "blah blah blah")
return response.end('invalid');
}
//Middleware 3
if (isMatch(request.url, "*")) {
return response.end(request.propB);
}
});
server.listen(1337);