gpt4 book ai didi

javascript - 检索node.js中的查询字符串参数

转载 作者:行者123 更新时间:2023-12-03 08:26:14 26 4
gpt4 key购买 nike

我正在尝试从 URL 检索查询字符串参数。我正在使用 Node.js Restify 模块。

网址如下所示;

http://127.0.0.1:7779/echo?UID=Trans001&FacebookID=ae67ea324&GetDetailType=FULL

提取的相关代码;

server.use(restify.bodyParser());

server.listen(7779, function () {
console.log('%s listening at %s', server.name, server.url);
});

server.get('/echo/:message', function (req, res, next) {
console.log("req.params.UID:" + req.params.UID);
console.log("req.params.FacebookID:" + req.params.FacebookID);
console.log("req.params.GetDetailType" + req.params.GetDetailType);

var customers = [
{name: 'Felix Jones', gender: 'M'},
{name: 'Sam Wilson', gender: 'M'},
];
res.send(200, customers);

return next();
});

如何修改代码,以便可以从 URL http://127.0.0.1:7779/echo?UID=Trans001&FacebookID=ae67ea324&GetDetailType=FULL 检索 req.params.UID 和其他参数?

最佳答案

使用req.query而不是req.params。您可以阅读here

server.use(restify.bodyParser());
server.use(restify.queryParser());

server.listen(7779, function () {
console.log('%s listening at %s', server.name, server.url);
});

server.get('/echo', function (req, res, next) {
console.log("req.query.UID:" + req.query.UID);
console.log("req.query.FacebookID:" + req.query.FacebookID);
console.log("req.query.GetDetailType" + req.query.GetDetailType);

var customers = [
{name: 'Felix Jones', gender: 'M'},
{name: 'Sam Wilson', gender: 'M'},
];
res.send(200, customers);

return next();
});

关于javascript - 检索node.js中的查询字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com