简体   繁体   English

如何使node.js本地主机公开

[英]How to make node.js localhost public

I am running node.js on port 3000 right now so it only loads on localhost:3000 on my computer. 我现在在端口3000上运行node.js,因此它仅在计算机上的localhost:3000上加载。 But I would like to test it on multiple computers. 但我想在多台计算机上进行测试。 How could I do this? 我该怎么办?

Here is the code: 这是代码:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
  console.log(socket.id)
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

The default behaviour is to listen on all available network interfaces, not just localhost, so the short answer is do nothing . 默认行为是在所有可用的网络接口上侦听,而不仅仅是在localhost上侦听,因此简短的答案是什么也不做

Any computer that can issue a network request to your computer can access the server by making an HTTP report to port 3000 on any IP address your computer has. 可以向您的计算机发出网络请求的任何计算机都可以通过向计算机上任何IP地址上的端口3000进行HTTP报告来访问服务器。

Most computers the development work is performed on do not have an ip address on the public Internet. 进行开发工作的大多数计算机在公共Internet上没有IP地址。 So for computers outside your local network to access the service, you would need to configure your Internet router to forward requests to some port it has available to port 3000 on your LAN IP address. 因此,要使本地网络外部的计算机访问该服务,您需要将Internet路由器配置为将请求转发到它可以用于LAN IP地址上的端口3000的某个端口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM