var express = require("express")
var app = express()
var router = express.Router()
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.send("401")
})
app.use("/", router)
app.listen(3000)
this is an example in the express official document.
1, router.use mounts a middleware to check the request header, and since no path and method are specified, all paths and methods of the request will execute it.
2. When I request localhost:3000/, I will output an incorrect web page without adding a x-auth request header. If I add a request header, I will output "hello, usernames". I can understand this part.
3. When a localhost:3000/admin is requested, 401 will be returned without a request header, but why do you add a request header and output a "hello, username request"? one is the request"/ "and the other is" / admin",.
4, use return next ("router") which side will this return to?