Skip to content

Instantly share code, notes, and snippets.

@gtalin
Created November 10, 2017 12:21
Show Gist options
  • Save gtalin/cbfdfea6ead32e2af65e39af16822d48 to your computer and use it in GitHub Desktop.
Save gtalin/cbfdfea6ead32e2af65e39af16822d48 to your computer and use it in GitHub Desktop.
Express middleware router-level
// server.js
var express = require('express');
var app = express();
var router = express.Router();
function date(req,res,next) {
console.log("Date is", Date());
next()
}
function foo1(req,res,next) {
console.log("App started#");
//res.send("App started#");
next();
}
function foo2(req,res,next) {
console.log("Request URl is::", req.originalUrl);
next();
}
function foo3(req,res,next) {
console.log("Request Type:", req.method);
next();
}
function cats(req,res,next) {
console.log("Cast of cats welcomes you");
res.send("Cast of cats welcomes you");
}
router.use(date);
router.use("/", foo1, foo2, foo3);
router.use("/cats", cats);
app.use("/meta", router);
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment