(node.js)定义没有表达的子域

时间:2014-10-12 17:05:16

标签: javascript node.js http localhost subdomain

您能解释一下如何为我的服务器创建一个子域(明确没有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控制台不打印任何内容,为什么?

是否有特定的过程来设置子域以便可以从Node访问?

1 个答案:

答案 0 :(得分:1)

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

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

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