Middleware is a huge word for such a small yet powerful interface.
And yes, the next() call was missing.
Most nodejs code does not use the else-clause, but early return
instead. Nicer to read, fewer to type and some benchmarks showed a
slight speed-improvement.
The code would then be like this:
express.use(function(req, res, next) {
if(req.headers.host === '
example.com') {
return res.send({'Location': '
http://www.example.com'+req.url}, 301)
}
next()
})
This should be the shortest implementation according to
http://expressjs.com/guide.html
and .send() of express.