简体   繁体   English

NodeJs 错误:端口已在使用:3000 使用 socket.io 并表示

[英]NodeJs Error: Port already in use:3000 using socket.io and express

I'm making a node.js application that uses socket.io and express.我正在制作一个使用 socket.io 和 express 的 node.js 应用程序。

The code looks like this-代码看起来像这样——

const express=require('express');
const app=express();
const http=require('http').Server(app);
app.use(express.static('public'));

const io=require('socket.io')(http,{
  cors:{origin:'*'}
});

http.listen(3000,()=>{
  console.log('listening to port 3000');
});

//when connection made 
io.on('connection',(socket)=>{
  console.log('connection made!');
});

Here,public is a folder container all the html files for the website.在这里,public 是一个包含网站所有 html 个文件的文件夹。

Now,when I run it locally on my pc,it works fine But when I deployed it on the glitch.com servers,i get this error:现在,当我在我的电脑上本地运行它时,它工作正常但是当我将它部署在 glitch.com 服务器上时,我得到这个错误:

Error: listen EADDREINUSE: address already in use :::3000

As far as i know,3000 is the only available port in glitch and I cannot use another port.据我所知,3000 是故障中唯一可用的端口,我不能使用其他端口。

Also,the questions on stack overflow related to this topic doesn't help me since most of the answers are related to killing the running tasks themselves and I don't have permission to do so on the server.此外,与此主题相关的堆栈溢出问题对我没有帮助,因为大多数答案都与自己终止正在运行的任务有关,而我无权在服务器上这样做。

This often happens when your server is still running in error, try switching listening to 3001 and see if there is still the error这种情况经常发生在你的服务器还在运行出错的时候,试试切换监听3001看看是否还是报错

free your 3000 port:释放你的 3000 端口:

Linux: fuser -k 3000/tcp Linux:定影器-k 3000/tcp

According to this question on glitch.com's forums you might have two services listening to the same port, but that doesn't seem to be the case.根据 glitch.com 论坛上的这个问题,您可能有两个服务侦听同一个端口,但情况似乎并非如此。

You should also be able to change the default listening port by setting the PORT environment variable in your .env file if you have one.您还应该能够通过在.env文件中设置PORT环境变量来更改默认侦听端口(如果有的话)。 Trying a different port should solve the problem尝试不同的端口应该可以解决问题

EDIT: Apparently the problem could be the initial configuration of the server, you can try setting everything up like this:编辑:显然问题可能出在服务器的初始配置上,您可以尝试像这样设置所有内容:

const app = require('express')();
const server = require('http').createServer(app); <--
const io = require('socket.io')(server);
io.on('connection', () => { /* your code */ });

server.listen(process.env.PORT, () => {/* your code */});

(Got this from socket.io github page ) (从socket.io github 页面得到这个)

If this doesn't work either you should try asking on glitch.com's support forums , one of their technicians will help you better troubleshoot your problem, they have all the necessary tools如果这也不起作用,您应该尝试在glitch.com 的支持论坛上提问,他们的一位技术人员将帮助您更好地解决问题,他们拥有所有必要的工具

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

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