8

如何处理 nodejs 的子域请求?

例如以下代码在控制台中对http://localhost:9876/[anything]上的任何请求进行回显测试

var http = require('http');

http.createServer(function(req, res){
    console.log("test");
}).listen(9876);

现在我想回答http://[anything].localhost:9876/[anything]上的任何请求, 它可以通过 apache 中的 htaccess来完成,NodeJS 中有什么替代方法?

4

1 回答 1

7

您的应用程序已经能够处理多个主机的请求。

由于您尚未指定 a hostname

[...] 服务器将接受定向到任何 IPv4 地址 ( INADDR_ANY) 的连接。

并且,您可以确定哪个host用于从以下位置发出请求headers

var host = req.headers.host; // "sub.domain:9876", etc.

不过,您还需要为 DNS 或hosts.

或者,使用诸如xip.io 之类的服务——使用 IP 地址而不是localhost

http://127.0.0.1.xip.io:9876/
http://foo.127.0.0.1.xip.io:9876/
http://anything.127.0.0.1.xip.io:9876/
于 2012-11-03T11:04:20.793 回答