Skip to content

Instantly share code, notes, and snippets.

@clonn
Created February 27, 2012 17:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save clonn/1925595 to your computer and use it in GitHub Desktop.
Save clonn/1925595 to your computer and use it in GitHub Desktop.
Express middleware simple example for node.js
/**
* @overview
*
* @author Caesar Chi
* @blog clonn.blogspot.com
* @version 2012/02/27
*/
var express = require('express'),
app = express.createServer(),
port = 1337;
function middleHandler(req, res, next) {
console.log("execute middle ware");
next();
}
app.use(function (req, res, next) {
console.log("first middle ware");
next();
});
app.use(function (req, res, next) {
console.log("second middle ware");
next();
});
app.get('/', middleHandler, function (req, res) {
console.log("end middleware function");
res.send("page render finished");
});
app.listen(port);
console.log('start server');
@sanbor
Copy link

sanbor commented Dec 12, 2013

👍 Thank you for the info, I was looking a way to execute the middleware, not just declare it.

@Vyomrana02
Copy link

what would be the output when following runs on root route??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment