我如何在http模块中侦听没有'/'的路径

时间:2019-01-09 11:37:49

标签: node.js json http

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

private async Task OnReadyAsync()
{
    // attempt to get the channel
    var channel = _client.GetChannel(_channelId);
    // pattern match to SocketMessageChannel, the standard type required for a channel to be a messageable one
    if (!(channel is ISocketMessageChannel msgChannel)) return;

    // await the GetMessageAsync task to get the result of this task
    var msg = await msgChannel.GetMessageAsync(_msgId);
    // print Content if msg exists
    if (msg != null) Console.WriteLine(msg.Content);
}

http请求看起来像这样:

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

我的node.js代码是:

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

很遗憾,我无法更改发出http.createServer(function (req, res) { console.log('sth') }).listen(3000); 请求的代码。

1 个答案:

答案 0 :(得分:0)

请参见以下问题: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