Node.js 无法在nodejs中获取查询参数
我有这个代理中间件应用程序。它不包括express.js Server.js包含以下内容:Node.js 无法在nodejs中获取查询参数,node.js,Node.js,我有这个代理中间件应用程序。它不包括express.js Server.js包含以下内容: const app = require('connect')(), http = require('http'), 而中间件则有一套规则,例如: const httpProxy = require('http-proxy'), HttpProxyRules = require('http-proxy-rules'), const proxyRules = new HttpProxyRules({
const app = require('connect')(),
http = require('http'),
而中间件则有一套规则,例如:
const httpProxy = require('http-proxy'),
HttpProxyRules = require('http-proxy-rules'),
const proxyRules = new HttpProxyRules({
rules: {
'/api/v1/stuff/([0-9]+)/documents/': 'http://0.0.0.0:3000/$1',
},
default: 'http://localhost:4443'
});
所以所有其他的微服务都被这个代理截获了。
有一个“app.use”可以进行一些检查。
在这里我可以看到请求对象。我对读取附加到url的查询参数感兴趣。
所以当我有了这个:
http://localhost:8081/api/v1.1/stuff/63/documents/file.pdf?token=mytoken
打印此文件:
console.log('GATEWAY',req.originalUrl);
将输出以下内容:
http://localhost:8081/api/v1.1/stuff/63/documents/file.pdf?token=mytoken
但是,如何访问查询参数?由于我没有使用express,执行“req.query”会给出未定义的结果。
我尝试了一系列的解决方案:“查询字符串”、“url”等,但它们给出了非常奇怪的结果,而且很难获得字段本身。我永远不会做这样的事情:
req.query
我看过connect文档,但没有关于获取请求查询参数的内容。
我应该用什么 请检查您是否使用了以下各项: 如果没有,请添加:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
并尝试使用npm获取req.params已安装的body解析器。在server.js中添加了这个:const bodyParser=require('body-parser');use(bodyParser.json());use(bodyParser.urlencoded({extended:true}));但是在代理中间件中,req.query和req.params都是未定义的。