我试图在我的nodeJS服务器URL中获取一个参数,如下所示:localhost /?name = Vincent
所以,我写了这段代码:
var request = require('request');
var http = require('http');
http.createServer(fybctuib (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
console.log(req.query.name) // or juste req.query
等
当我进入localhost时,我在日志中未定义... 请问你能帮帮我吗 ?谢谢。
答案 0 :(得分:2)
您正在使用Node.js的http
模块。如果您查看IncomingMessage
对象的文档,则会发现它没有query
属性。您可能已经看过Express.js的示例,并且将它们与本机http
模块混淆。
要么使用Express.js,要么使用the message.url
documentation中所示的url.parse
。