From the Express docs…
A middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application.
Again, but with more details.
req
)res
)next
)This should sound pretty familiar to you. It's the same kind of function that we pass to our app.VERB
methods.
app.get('/', function(req, res) {
res.send('Familiar, no?');
});
Middleware can be:
You can use middleware simply by calling the aptly named:
// for the whole application:
app.use(yourMiddleWareFunction);
// or... for a specific path:
app.use('path', yourMiddleWareFunction);
Can I have multiple middleware? →
The fact that there's a next
parameter implies… YES!
But what order is the middelware executed in? →
So, you say you want to make your own middleware, eh? What can you do in your fancy middleware function? →
Ohhhh. Is there a danger with calling render or send in middleware then? →
You can end up skipping other chained middleware.
Furthermore, if the current middleware doesn't end the request-response cycle, it should really remember to call next()
. What will happen if it doesn't? →
Create middleware that always logs the word 'hello' for every request. →
app.use(function(req, res, next) {
console.log('hello');
next();
});
Create middleware that always logs the request's method and path. →
app.use(function(req, res, next) {
console.log(req.method, req.path);
next();
});
(also… let's play around with ordering. →)
Does anyone remember a response header that identifies the type of server that's being run? →
Server:Apache/2.2.22 (Ubuntu)
How about we set our own Server
response header?
We can use res.set(headerName, headerValue)
.
app.use(function(req, res, next) {
res.set('Server', 'MY AMAZING SUPER COOL SERVER');
next();
});
Actually, most people try to suppress or remove that Server header. Why? →
Security through obscurity!
const express = require('express');
const app = express();
app.use(function(req, res, next) {
console.log(req.method, req.path);
next();
});
app.use(function(req, res, next) {
console.log('hello');
next();
});
app.use(function(req, res, next) {
res.set('Server', 'MY AMAZING SUPER COOL SERVER');
next();
});
app.get('/', function(req, res) {
res.send('We\'re done here');
});
app.listen(3000);
Hey. So… DIY is cool and all, but making our own middleware seems like a lot of work. Are there any pre-built middleware out there? (hint we've used one, and we've seen others) →
Some middleware that we've either seen or actually used:
What do you think the static file middleware does? How does it work in the request-response life cycle? →
How do we actually use the static file middleware? →
app.use(express.static('public'));
const path = require('path');
const publicPath = path.resolve(__dirname, 'public');
app.use(express.static('public'));
By the way… if we were using our logging middleware from before, how do we make sure that requests handled by the static file middleware are actually logged? →
Just make sure that logging is included (app.use
) before static files.
The body parser middleware:
app.use(bodyParser.urlencoded({ extended: false }));
POST requests send their data in the body. How can we issue a POST request? →
request
)
Sooooo… maybe it's time to figure out what this form thing is all about. →