简体   繁体   English

如何使用 nodejs 启动服务器?

[英]How do I start a server using nodejs?

I started learning nodejs and stopped at the lesson where the server is created, here is the code for this script:我开始学习 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..')

I experiment on a remote machine (google cloud virtual machine), run the script using node (I see a message in the console that the server is running) but if I go to the IP address through the browser (for example http://92.233.12.12:5000/ ) I don't I see the result, what am I doing wrong?我在远程机器(谷歌云虚拟机)上进行实验,使用 node 运行脚本(我在控制台中看到服务器正在运行的消息)但是如果我通过浏览器访问 IP 地址(例如http:// 92.233.12.12:5000/ ) 我没有看到结果,我做错了什么? did not find any additional information, everywhere in the lessons access through localhost:5000/ ...没有找到任何额外的信息,在课程中到处都是通过localhost:5000/访问...

res.write('<html><body><p>This is admin Page.</p></body></html>');
res.end();

You can also do the following:您还可以执行以下操作:

res.end('<html><body><p>This is admin Page.</p></body></html>');

Please check if you are able to telnet the port 5000 for the remote server IP from your laptop terminal.请检查您是否能够从您的笔记本电脑终端 telnet 远程服务器 IP 的端口 5000。 It could be that a firewall is blocking the port for the outside access.可能是防火墙阻止了外部访问的端口。 If you want to check this on the server side you can login to the google machine and from the terminal of the server try curl http://localhost:5000 and if you see the response , then it should be the firewall.如果你想在服务器端检查这个,你可以登录谷歌机器并从服务器的终端尝试 curl http://localhost:5000 ,如果你看到响应,那么它应该是防火墙。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Expressjs / nodejs中启动时调用函数(Setup方法) - How do I call a function at start in Expressjs/nodejs (Setup method) 我如何使用Node.js在服务器上接收与客户端发布数据格式相同的发布数据? - How do i receive post data at server same as client post data format using nodejs? 使用socketIO可访问nodeJS服务器时,如何显示给用户? - How do I display to the user when the nodeJS server is reachable using socketIO? 如何与Node.js服务器中的本机可执行文件通信? - How do I communicate with a native executable from a nodejs server? 如何在Nodejs服务器上安装自己的字体? - How do I install my own fonts on Nodejs Server? Bazel:我如何使用nodeJS_binary规则来执行“npm run start” - Bazel: How do i use nodeJS_binary rule to do “npm run start” 我想上传 1 个文件并克隆到 3 个文件,如何在 nodejs 中使用 multer 将多个文件大小上传到服务器 - I want to upload 1 file and clone to 3 files and how do I upload multiple file sizes to the server using multer in nodejs 使用 nodeJS 单击获取数组索引/如何删除 nodeJS 服务器上的留言簿条目 - Get array index on click with nodeJS / How do I delete a guestbook entry on my nodeJS server 如何在Rdio Web回放API示例中启动服务器? - How do I start the server in the Rdio web playback API example? 在使用网络摄像头输入进行面部识别(opencv4nodejs)时,如何在服务器(javascript)上记录和下载/上传网络摄像头 stream? - How do I record AND download / upload the webcam stream on server (javascript) WHILE using the webcam input for facial recognition (opencv4nodejs)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM