Node.js 节点10中未定义request.path
但是,根据,它不在节点Node.js 节点10中未定义request.path,node.js,http,Node.js,Http,但是,根据,它不在节点v10.13.0 const http = require('http') require('../src/shared/globals.js') const port = 4000 const host = '127.0.0.1' const server = http.createServer(function(request, response) { log(`>>>> url ${request.url}`) log(`&g
v10.13.0
const http = require('http')
require('../src/shared/globals.js')
const port = 4000
const host = '127.0.0.1'
const server = http.createServer(function(request, response) {
log(`>>>> url ${request.url}`)
log(`>>>> path ${request.path}`)
})
module.exports = async function() {
server.listen(port, host, function() {
console.log(`Web hook server listening at http://${host}:${port}`)
})
}
将返回:
>>>> url /
>>>> path undefined
为什么request.path未定义 它包含在文档中,但在“ClientRequest”部分下。我的理解是,它是在您想要调用服务器时使用的。它未定义,因为回调中的“请求”不是ClientRequest 您可以使用url模块解析您正在获取的“request.url”。这里有一个例子 编辑:这里有一些有用的链接 Node.js
url.parse()
链接:
Node.js客户端请求文档:
Node.js
request.path
Doc:您在其他版本中进行了测试,以证明这不是版本问题,对吗?从你回答问题的方式来看,这几乎就像你在说这是特定于该节点版本的问题,但这不太可能。@KevinB不,我没有对该问题进行回溯测试。我没有说或暗示这是一个版本问题,我包含了版本号,因为这是一个很好的实践——例如,可能文档不适用于节点10。你是不是因为我没有回溯测试而对这个问题不感兴趣?谢谢@Plpicard。如果您为http.createServer
提供的请求添加到文档的链接,我会将此标记为答案。我正在寻找request
提供给http.createServer
的链接,而不是您提到的无关的clientRequest。OP寻找的答案是request
参数是http.IncomingMessage的实例