简体   繁体   中英

how to catch request in express + nodejs?

could you please tell me how to catch request in express + nodejs ? I want to catch all request like /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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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