简体   繁体   English

表达api,为什么会出现错误:监听EADDRINUSE ::: 5000

[英]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 )。

Maybe you can use Nodemon 也许您可以使用Nodemon

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

相关问题 错误:监听 EADDRINUSE:地址已在使用:::5000 但 dotnet 服务器确实让我在端口 5000 中运行 api - Error: listen EADDRINUSE: address already in use :::5000 BUT dotnet server does let me run api in port 5000 错误:监听 EADDRINUSE:地址已在使用:::5000 - Error: listen EADDRINUSE: address already in use :::5000 Nodemon:错误:收听 EADDRINUSE:地址已在使用中:::5000 - Nodemon: Error: listen EADDRINUSE: address already in use :::5000 为什么我反复收到 EADDRINUSE 错误 - Why do I repeatedly get EADDRINUSE errors 听 EADDRINUSE:地址已被使用 :::5000 - listen EADDRINUSE: address already in use :::5000 npm start 出错。 错误:监听 EADDRINUSE:地址已在使用 :::5000 - ERROR with npm start. Error: listen EADDRINUSE: address already in use :::5000 如何修复“错误:监听 EADDRINUSE:地址已在使用中:::5000”未处理的“错误”事件 - How to fix "Error: listen EADDRINUSE: address already in use :::5000" Unhandled 'error' event 错误:尝试运行Express.js应用程序时听EADDRINUSE吗? - Error: listen EADDRINUSE when trying to run an Express.js application? 带有 Express 的 Node.JS:错误:监听 EADDRINUSE:地址已在使用中 - Node.JS with Express: Error : listen EADDRINUSE: address already in use 错误:在nodejs中监听EADDRINUSE - Error: listen EADDRINUSE in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM