简体   繁体   English

Node.js 使用与分配不同的侦听端口

[英]Node.js using different listening port than assigned

I set socket.io to listen to port 4000. When I view my app from my browser, the page will not load on port 4000. However, app will load on port 5500. Could not find where port 5500 was assigned.我将 socket.io 设置为侦听端口 4000。当我从浏览器查看我的应用程序时,该页面不会在端口 4000 上加载。但是,应用程序将在端口 5500 上加载。找不到端口 5500 的分配位置。

 const mongo = require('mongodb').MongoClient; const client = require('socket.io').listen(4000).sockets; const dbName = 'chats'; //Database collection // Connect to mongo mongo.connect('mongodb://127.0.0.1/mongochat', function(err, client) { if (err) { throw err; } console.log('MongoDB connected...'); // Connect to Socket.io client.on('connection', function(socket) { let chat = client.db(dbName);

According to Socket.io documentation ( https://socket.io/docs/server-api/ )根据 Socket.io 文档( https://socket.io/docs/server-api/

Basic基本的

const io = require('socket.io')(3000);

With options有选项

const io = require('socket.io')(3000, {
  path: '/test',
  serveClient: false,
  // below are engine.IO options
  pingInterval: 10000,
  pingTimeout: 5000,
  cookie: false
});

⚠️ Note ⚠️注意

In case you are using some network package (like express or http ), check that those packages are not overriding your listening port.如果您使用某些网络 package (如expresshttp ),请检查这些软件包是否没有覆盖您的侦听端口。

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

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