【问题标题】:how can I listen a path without '/' in http module如何在 http 模块中收听没有“/”的路径
【发布时间】:2019-06-04 04:14:04
【问题描述】:

当 http 请求如下所示时,我可以触摸我的代码:

POST /init HTTP/1.1  
Host: 127.0.0.1  
Connection: keep-alive  
Content-Length: 1343 

http 请求如下所示:

POST init HTTP/1.1 // what different is : the path without a '/'  
Host: 127.0.0.1  
Connection: keep-alive  
Content-Length: 1343  

我的 node.js 代码是:

http.createServer(function (req, res) {
  console.log('sth')
}).listen(3000); 

很遗憾,我无法更改发出 init 请求的代码。

【问题讨论】:

  • 有趣...我想知道这是否被认为是有效的 HTTP 请求?
  • 你是对的.. 但我的客户不想修复它,所以我想适应这种情况。

标签: node.js json http


【解决方案1】:

请看这个问题:Do HTTP paths have to start with a slash?

显然,是的,起始斜杠是有效的 HTTP 请求所必需的。

由于这在技术上是无效的 HTTP,因此大多数库不支持它。

似乎 Apache 可以容忍不带斜线的路径...也许将其用作反向代理作为解决方法?

另一种可能性是分叉 HTTP 服务器库并对其进行修改以容忍不带斜杠的路径...您可能想看看这篇文章:https://softwareengineering.stackexchange.com/questions/349656/implement-a-http-server-on-top-of-nodejs-tcp-apis-for-learning-purposes

【讨论】:

  • 我知道。它必须以斜线开头..但我的客户不想修复它。哭..哭..哭
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多