最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

request.path在节点10中未定义

运维笔记admin9浏览0评论

request.path在节点10中未定义

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论