Created
November 10, 2017 12:21
-
-
Save gtalin/cbfdfea6ead32e2af65e39af16822d48 to your computer and use it in GitHub Desktop.
Express middleware router-level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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