Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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_Http_Server_Nodejs Server - Fatal编程技术网

Javascript 如何使用nodejs启动服务器?

Javascript 如何使用nodejs启动服务器?,javascript,node.js,http,server,nodejs-server,Javascript,Node.js,Http,Server,Nodejs Server,我开始学习nodejs,在创建服务器的课程中停了下来,下面是这个脚本的代码: var http = require('http'); // Import Node.js core module var server = http.createServer(function (req, res) { //create web server if (req.url == '/') { //check the URL of the current request

我开始学习nodejs,在创建服务器的课程中停了下来,下面是这个脚本的代码:

var http = require('http'); // Import Node.js core module

var server = http.createServer(function (req, res) {   //create web server
    if (req.url == '/') { //check the URL of the current request
        
        // set response header
        res.writeHead(200, { 'Content-Type': 'text/html' }); 
        
        // set response content    
        res.write('<html><body><p>This is home Page.</p></body></html>');
        res.end();
    
    }
    else if (req.url == "/student") {
        
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><p>This is student Page.</p></body></html>');
        res.end();
    
    }
    else if (req.url == "/admin") {
        
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><p>This is admin Page.</p></body></html>');
        res.end();
    
    }
    else
        res.end('Invalid Request!');

});

server.listen(5000); //6 - listen for any incoming requests

console.log('Node.js web server at port 5000 is running..')
var http=require('http');//导入Node.js核心模块
var server=http.createServer(函数(req,res){//createweb服务器
如果(req.url=='/'){//,请检查当前请求的url
//设置响应头
res.writeHead(200,{'Content-Type':'text/html'});
//设置响应内容
res.write(“这是主页。

”); res.end(); } else if(req.url==“/student”){ res.writeHead(200,{'Content-Type':'text/html'}); res.write(“这是学生页面。

”); res.end(); } else if(req.url==“/admin”){ res.writeHead(200,{'Content-Type':'text/html'}); res.write(“这是管理页面。

”); res.end(); } 其他的 res.end('无效请求!'); }); 服务器。听(5000)//6-侦听任何传入的请求 log('端口5000处的Node.js web服务器正在运行..')
我在远程机器(谷歌云虚拟机)上进行实验,使用node运行脚本(我在控制台中看到服务器正在运行的消息),但如果我通过浏览器(例如)访问IP地址,我看不到结果,我做错了什么?在课程中的任何地方都找不到任何其他信息,请通过localhost:5000/..

res.write(“这是管理页面。

”);
res.write('<html><body><p>This is admin Page.</p></body></html>');
res.end();
res.end();
您还可以执行以下操作:

res.end('<html><body><p>This is admin Page.</p></body></html>');
res.end(“这是管理页面。

”);
res.write(“这是管理页面。

”); res.end();
您还可以执行以下操作:

res.end('<html><body><p>This is admin Page.</p></body></html>');
res.end(“这是管理页面。

”);

请检查您是否能够从笔记本电脑终端远程登录远程服务器IP的端口5000。可能是防火墙阻止了外部访问的端口。如果你想在服务器端检查这一点,你可以登录到谷歌机器,然后从服务器的终端尝试curlhttp://localhost:5000 如果您看到响应,那么应该是防火墙。

请检查您是否能够从笔记本电脑终端远程连接远程服务器IP的端口5000。可能是防火墙阻止了外部访问的端口。如果你想在服务器端检查这一点,你可以登录到谷歌机器,然后从服务器的终端尝试curlhttp://localhost:5000 如果看到响应,则应该是防火墙。

尝试在
0.0.0.0
上侦听。将侦听代码更改为类似于server.listen('0.0.0.0',5000)的代码尝试过,结果“无法访问站点”是您的虚拟机VPC配置为允许从该ip到端口的请求?我没有配置任何其他内容尝试在
0.0.0
上侦听。将侦听代码更改为类似于server.listen('0.0.0.0',5000)的代码尝试过,结果“无法访问站点”是您的虚拟机VPC配置为允许从该ip到端口的请求?我没有额外配置任何内容感谢您的建议,但这并没有解决我的问题problem@nikobazylev如果没有这些if语句,页面是否能完美显示?如果没有“if”-它也无法工作,在我看来,这并不是问题所在error@nikobazylev你的ip正确吗?您是否尝试导航到“”?我无法转到localhost,因为计算机是远程的,例如提供了IP,但我输入了正确的地址谢谢您的建议,但这并不能解决我的问题problem@nikobazylev如果没有这些if语句,页面是否能完美显示?如果没有“if”-它也无法工作,在我看来,这并不是问题所在error@nikobazylev你的ip正确吗?您是否尝试导航到“”?我无法转到localhost,因为计算机是远程的,例如提供了IP,但我输入了正确的地址Telnet说:“连接到IP.IP.IP.IP…无法打开到该节点的连接,在端口5000上:连接失败”。在服务器上添加了对防火墙中端口5000的权限,但仍然存在相同的错误您是否也使用命令curl从远程服务器终端进行了检查?您可以在这里发布命令的结果吗?当通过“curl localhost:5000”从服务器请求时,响应会附带页面内容-即正确的内容:“这是主页。

”,因此可以清楚地表明,问题是因为您无法使用IP访问端口5000上的GCP服务器。这可能主要是由于防火墙问题。您必须检查GCP文档,以允许在您使用的IP上使用端口5000,这将解决您的问题。或者,如果您想通过浏览器进行测试,请在GCP服务器上安装chrome或firefox,并使用xterm在远程服务器(如果服务器是linux)上打开浏览器,并且应该使用TELNET在该浏览器上工作:“连接到IP.IP.IP.IP…无法打开到该节点的连接,在端口5000上:连接失败”。在服务器上添加了对防火墙中端口5000的权限,但仍然存在相同的错误您是否也使用命令curl从远程服务器终端进行了检查?您可以在这里发布命令的结果吗?当通过“curl localhost:5000”从服务器请求时,响应会附带页面内容-即正确的内容:“这是主页。

”,因此可以清楚地表明,问题是因为您无法使用IP访问端口5000上的GCP服务器。这可能主要是由于防火墙问题。您必须检查GCP文档,以允许在您使用的IP上使用端口5000,这将解决您的问题。或者,如果您想通过浏览器进行测试,请在GCP服务器上安装chrome或firefox,并使用xterm在远程服务器(如果服务器是linux)上打开浏览器,并且应使用