简体   繁体   English

如何在 express + nodejs 中捕获请求?

[英]how to catch request in express + nodejs?

could you please tell me how to catch request in express + nodejs ?你能告诉我如何在 express + nodejs 中捕获请求吗? I want to catch all request like /in/docs , /es/docs , /fr/docs ..我想捕获所有请求,如/in/docs/es/docs/fr/docs ..

const server = express()

server.get('/in/docs', (req, res) => {
    console.log('====kk==')
    app.render(req, res, '/')
})
server.get('*', (req, res) => {
    return handle(req, res)
})

currently, I am doing hard cording like this it only works with /in .I want to catch all request /fr , /en .目前,我正在做这样的硬编码,它只适用于/in 。我想捕获所有请求/fr/en

server.get('/in/docs', (req, res) => {
    console.log('====kk==')
    app.render(req, res, '/')
})

Use route parameters using colons:使用冒号使用路由参数

server.get('/:language/docs', (req, res) => {
  console.log(req.params.language); // gives 'in' if you specify 'in', 'fr' if you specify 'fr', etc
});

You can nest parameters as much as you like, and that will give you an object in the req.params field.您可以req.params嵌套参数,这将在req.params字段中为您提供一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM