简体   繁体   中英

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. 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 .

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.

Most computers the development work is performed on do not have an ip address on the public Internet. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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