gpt4 book ai didi

node.js - nodejs中的子域

转载 作者:搜寻专家 更新时间:2023-10-31 22:32:29 24 4
gpt4 key购买 nike

如何处理 nodejs 的子域请求?

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

var http = require('http');

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

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

最佳答案

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

因为您没有指定 hostname :

[...] the server will accept connections directed to any IPv4 address (INADDR_ANY).

并且,您可以确定使用哪个主机headers 发出请求:

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

不过,您还需要为 DNS 或 hosts 中的子域配置 IP 地址.

或者,使用类似 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/

关于node.js - nodejs中的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13208145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com