什么是使用node.js在express.js中读取params的正确语法

时间:2014-01-17 02:13:49

标签: javascript node.js express

我有一个网址,例如

  http://localhost:8080/siteinfo.json?site&locationid=1&companyid=1

我想要解决一个功能并通过?相形见绌。什么是正确的语法?

  app.get('/siteinfo.json', getdata_hdlr.get_site_setup);

2 个答案:

答案 0 :(得分:3)

如果你没有在你的路线中定义你的参数,那么它将是:

req.param('locationid');

但您可以在路线中创建占位符,例如:

app.get("/product/:id", product.show);

然后,'id'参数在控制器中可用:

req.params.id

答案 1 :(得分:2)

查询参数已经在请求对象中。

app.get('/siteinfo.json', function(req, res) {
  console.log(req.query);
  res.send("locationid="+req.query.locationid+"\ncompanyid="+req.query.companyid);
});
相关问题