Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs上的服务器_Javascript_Node.js - Fatal编程技术网

Javascript nodejs上的服务器

Javascript nodejs上的服务器,javascript,node.js,Javascript,Node.js,我有一个运行静态index.html文件服务器的小脚本: 是否可以使用字符串而不是“localhost:3000”(例如“MyPAGE”)来调用html文件?localhost只不过是本地服务器IP,即127.0.0.1要使用MyPAGE更改字符串localhost,您需要根据本地服务器IP注册主机名 窗户 编辑c:\Windows\System32\Drivers\etc\hosts并添加127.0.0.1 MyPAGE 注意:您需要以管理员身份打开此文件 对于linux 编辑-/etc/ho

我有一个运行静态index.html文件服务器的小脚本:


是否可以使用字符串而不是“localhost:3000”(例如“MyPAGE”)来调用html文件?

localhost只不过是本地服务器IP,即127.0.0.1要使用MyPAGE更改字符串localhost,您需要根据本地服务器IP注册主机名

窗户 编辑c:\Windows\System32\Drivers\etc\hosts并添加127.0.0.1 MyPAGE 注意:您需要以管理员身份打开此文件

对于linux 编辑-/etc/host文件并添加127.0.0.1 MyPAGE

URL由以下内容组成

在您的示例中:

该方案被省略,但浏览器隐式地添加http://因为您正在地址栏中键入它。 授权由主机名和端口组成 主机名是localhost 港口是3000 路径被省略,默认为/ 其他所有内容都被省略,没有内容 要使用MyPage,您需要:

端口为80,这是HTTP的默认端口,因此只有在使用默认端口时才能忽略它 在这种情况下,您可以使用浏览器将主机名MyPage解析为与localhost 127.0.0.1相同的IP地址 因此,您需要:

更改服务器.listen3000,'127.0.0.1';到server.listen80,“127.0.0.1”;。请记住,端口80是一个帐户,因此您需要是root/管理员帐户才能在那里运行服务。 配置DNS或/etc/hosts,以便MyPage解析到正确的IP地址。
你是说http://localhost:3000/MyPAGE? 或者您想在浏览器地址栏中键入MyPAGE?谢谢您的回复,我的意思是“MyPAGE”,没有其他内容!最好使用web服务器,如nginx或apache。
var http = require('http');
var fs =require('fs');


var server = http.createServer(function(req,res){
    console.log('request was made : '+req.url);
    res.writeHead(200,{'Content-Type':'text/html'});
    var myReadStream  = fs.createReadStream(__dirname +'/index.html','utf8');
    myReadStream.pipe(res);
});

server.listen(3000,'127.0.0.1');

console.log('listening to 3000');
     foo://example.com:8042/over/there?name=ferret#nose
     \_/   \______________/\_________/ \_________/ \__/
      |           |            |            |        |
   scheme     authority       path        query   fragment
      |   _____________________|__
     / \ /                        \
     urn:example:animal:ferret:nose