您现在的位置是:首页 > 文章详情  网站首页文章详情

nodejs中使用url的path参数

  • Administrator
  • 250
  • 2020-04-24 10:11:07
  • Nodejs
简介链接: http://localhost:3002/voice/id/5d22e14b73965e03e41134b4 路由 // 获取声音并生成链接 router.get('/id/:id', (req, res)=>{ console.log(req....

链接: http://localhost:3002/voice/id/5d22e14b73965e03e41134b4 路由

// 获取声音并生成链接

router.get('/id/:id', (req, res)=>{
    console.log(req.params.id);
    res.render('voice/voice');
});

此时可以正常获取id的值 以下是官方文档

The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the object is always referred to as req (and the HTTP response is res) but its actual name is determined by the parameters to the callback function in which you’re working.

For example:

app.get('/user/:id', function (req, res) {
  res.send('user ' + req.params.id)
})

But you could just as well have:

app.get('/user/:id', function (request, response) {
  response.send('user ' + request.params.id)
})

The req object is an enhanced version of Node’s own request object and supports all built-in fields and methods.

文章评论

Top