request.path在节点10中未定义
根据node docs, request.path
should exist,但它不在节点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”部分。我的理解是,当你想要调用服务器时使用它。它是未定义的,因为回调中的'request'不是ClientRequest。
您可以使用url模块来解析您获得的'request.url'。这是一个例子https://www.codexpedia/node-js/node-js-http-request-url-param-path-and-body/
编辑:这是一些有用的链接。
Node.js url.parse()
链接:https://nodejs/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
Node.js客户端请求Doc:https://nodejs/api/http.html#http_class_http_clientrequest
Node.js request.path
Doc:https://nodejs/api/http.html#http_request_path