在express.js中使用node.js读取params的正确语法是什么?
我有一个url如
http://localhost:8080/siteinfo.json?site&locationid=1&companyid=1
我想要一个函数,并通过? 它的参数。 什么是正确的语法?
app.get('/siteinfo.json', getdata_hdlr.get_site_setup);
如果你还没有在你的路线中定义你的参数,那么它将是:
req.param('locationid');
但是您可以在您的路线中创build占位符,例如:
app.get("/product/:id", product.show);
那么这个“ID”参数可以在你的控制器中使用:
req.params.id
查询参数已经在请求对象中。
app.get('/siteinfo.json', function(req, res) { console.log(req.query); res.send("locationid="+req.query.locationid+"\ncompanyid="+req.query.companyid); });