[英]express api, why do I get Error: listen EADDRINUSE :::5000
I'm creating a simple express api using this code 我正在使用此代码创建一个简单的Express API
//server.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const db = require('./config/keys').mongoURI;
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log('db connected'))
.catch(err => console.error(err))
const port = process.env.PORT || 5000;
app.listen(port, () => console.log('server connected'))
//config/keys.js
module.exports = {
mongoURI: 'mongodb://db address'
};
When I run it or make changes to the file I always get an error 当我运行它或更改文件时,总是出现错误
Error: listen EADDRINUSE :::5000
at Server.setupListenHandle [as _listen2] (net.js:1327:14)
at listenInCluster (net.js:1375:12)
at Server.listen (net.js:1462:7)
at Function.listen (/Users/user/Desktop/mern-2/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/user/Desktop/mern-2/server.js:18:5)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1354:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
I can fix this by running lsof -i :5000
and then kill
the node returned 我可以通过运行
lsof -i :5000
来解决此问题,然后kill
返回的节点
Why do I get this error and is there a way to stop it 为什么我会收到此错误,并且有办法阻止它
The error Error: listen EADDRINUSE :::5000
is self descriptive. 错误
Error: listen EADDRINUSE :::5000
是自我描述的。
Looks like another application is listening to the port 5000
, or another instance of your same app is already running in the system. 似乎另一个应用程序正在侦听端口
5000
,或者同一应用程序的另一个实例已在系统中运行。
Your solution of killing the process works - because there you kill the process which is listening to the port 5000. 您的终止进程的解决方案有效-因为在那里您终止了监听端口5000的进程。
It is not immediately clear from the information given if it is another instance of your own app or if it is a different app which uses the port 5000. 从给出的信息中并不能立即得知它是您自己的应用程序的另一个实例还是使用端口5000的其他应用程序。
If it is another app (which you can figure out by looking at lsof -i -n -P
), you should use another port. 如果它是另一个应用程序(您可以通过查看
lsof -i -n -P
找出来),则应使用另一个端口。
If it is another instance of your own app, make sure you terminate the running instance ( Ctrl + C
) before editing. 如果它是您自己的应用程序的另一个实例,请确保在编辑之前终止正在运行的实例(
Ctrl + C
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.