【问题标题】:(node.js) defining subdomains without express(node.js) 不使用 express 定义子域
【发布时间】:2014-12-07 07:03:00
【问题描述】:

你能解释一下如何为我的服务器创建子域(明确没有 Express)吗?

为什么如果我执行以下操作,请求不会显示子域?

http = require("http");

server = http.createServer(function(req, res) {
   console.log(req.url); //should prints "/" (because a subdomain should be considered like a new website)
   console.log(req.headers.host); //should prints "sub.localhost"       
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end("Ok");
}).listen(80);

现在,如果我导航到 http://sub.localhost/ chrome 显示“此网页不可用”并且从 node.js 控制台不打印任何内容,为什么?

是否有特定的程序来设置子域以便可以从节点访问?

【问题讨论】:

    标签: javascript node.js http localhost subdomain


    【解决方案1】:

    确保您在操作系统的hosts 文件中设置了sub.localhost(例如127.0.0.1 sub.localhost),否则它可能不知道如何解析该主机名。

    在 Windows 上,hosts 文件通常为 %WINDIR%\System32\drivers\etc\hosts

    在所有其他平台上,hosts 文件通常为 /etc/hosts

    【讨论】:

    • 你是最棒的。可以很好地编辑该文件,非常感谢:)
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2013-11-20
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多