Node (Express) 中间件——如何正确地连接到不同的端点
Node (Express) middlewares - how to properly tie back to different endpoints
我开始接触 Express,他们有中间件的这个特性,我目前正在探索。
假设我有 2 个端点(/middlewaredemoa
、/middlewaredemob
),它们都需要一些不同的初始处理。然后,无论初始终点如何,他们都有一个适用于相同治疗的通用中间件。
在使用 GET() 提供响应时,我不确定自己是否做对了。在我看来,如果我有 2 个不同的端点,它们每个都需要自己的 app.get("/middlewaredemoa", ...)
。一定如此吗?假设我只是对一个变量进行一些处理,然后我需要 return 该 var 最终处于哪种状态,我仍然最好为每个端点定义 1 个 get() 吗?
let response = ""
app.use('/middlewaredemoa', (req, resp, next) => {
response = "Greetings from: M1a"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M1a gets req and passes it on to two")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
app.use('/middlewaredemob', (req, resp, next) => {
response = "Greetings from: M1b"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M1b gets req and passes it on to two")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
//some common treatment for all the above endpoints
app.use((req, resp, next) => {
response += " and M2"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M2 gets req and passes it on to GET()")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
//get - need 1 get per endpoint?
app.get("/middlewaredemoa",(req,resp) => {
console.log("GET() handler after the middlewares")
resp.send(response + " and GET() 1")
})
app.get("/middlewaredemob",(req,resp) => {
console.log("GET() handler after the middlewares")
resp.send(response + " and GET() 2")
})
/*ERROR HANDLING - could use same pattern as above to chain errors*/
app.use((err, req, resp, next) => {
console.log("CRASH BOOM UH!:"+err)
resp.status(500).send("Poop hit the fan HARD")
})
Express 提供了一种使用数组来匹配多个路由的方法。
像这样:
req.get(['/routea', '/routeb'], (req, res, next) => {
// do something
// res.send() if you want to send a response right away
// next() if you want to go to the next middleware
});
req.get
只是一个类似于 req.use
的中间件,只是它只匹配 req.method === 'GET'
编辑:评论中的奖励问题。
它只是一个中间件。使用与普通中间件相同的缩放技术。
我开始接触 Express,他们有中间件的这个特性,我目前正在探索。
假设我有 2 个端点(/middlewaredemoa
、/middlewaredemob
),它们都需要一些不同的初始处理。然后,无论初始终点如何,他们都有一个适用于相同治疗的通用中间件。
在使用 GET() 提供响应时,我不确定自己是否做对了。在我看来,如果我有 2 个不同的端点,它们每个都需要自己的 app.get("/middlewaredemoa", ...)
。一定如此吗?假设我只是对一个变量进行一些处理,然后我需要 return 该 var 最终处于哪种状态,我仍然最好为每个端点定义 1 个 get() 吗?
let response = ""
app.use('/middlewaredemoa', (req, resp, next) => {
response = "Greetings from: M1a"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M1a gets req and passes it on to two")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
app.use('/middlewaredemob', (req, resp, next) => {
response = "Greetings from: M1b"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M1b gets req and passes it on to two")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
//some common treatment for all the above endpoints
app.use((req, resp, next) => {
response += " and M2"
if ( Math.floor((Math.random() * 10) + 1) >=4){
console.log("M2 gets req and passes it on to GET()")
next()
} else {
throw "ARRRRRRRRRRRRRGGGGHHHHHH!"
}
})
//get - need 1 get per endpoint?
app.get("/middlewaredemoa",(req,resp) => {
console.log("GET() handler after the middlewares")
resp.send(response + " and GET() 1")
})
app.get("/middlewaredemob",(req,resp) => {
console.log("GET() handler after the middlewares")
resp.send(response + " and GET() 2")
})
/*ERROR HANDLING - could use same pattern as above to chain errors*/
app.use((err, req, resp, next) => {
console.log("CRASH BOOM UH!:"+err)
resp.status(500).send("Poop hit the fan HARD")
})
Express 提供了一种使用数组来匹配多个路由的方法。
像这样:
req.get(['/routea', '/routeb'], (req, res, next) => {
// do something
// res.send() if you want to send a response right away
// next() if you want to go to the next middleware
});
req.get
只是一个类似于 req.use
的中间件,只是它只匹配 req.method === 'GET'
编辑:评论中的奖励问题。 它只是一个中间件。使用与普通中间件相同的缩放技术。