learning express.Router()
code:
const express = require('express'); const app = express(); var router = express.Router(); var cookieParser = require('cookie-parser'); app.use(cookieParser()); router.use(function (req, res, next) { //if (!req.headers['x-auth']) return next('router'); //next(); }) router.get('/', function(req, res){ res.send('hello, user!'); }); app.use('/admin', router, function (req, res) { res.sendStatus(401); }); // app.use(function(err, req, res, next){ // console.error(err.stack); // res.status(500).send('something broke'); // }); app.listen(3000);
result: