Reputation: 801
So, I have a simple express server which connects to the mongoDB atlas by mongoose. And for example I starting the server, everything works fine but when I do restart(no matter with nodemon or just by using node) I got an error:
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at Function.listen (C:\Users\Олег\e-commerce-mern\MERN-shop-app\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Олег\e-commerce-mern\MERN-shop-app\backend\server.js:36:20)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 5000
}
and after restarting one or few times my server starts working just fine without the error, but then, after saving again the problem repeats and i don't know why. db.js:
const mongoose = require('mongoose');
const mongoConnection = () => {
return mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});
};
module.exports = mongoConnection;
server.js:
const express = require('express');
const products = require('./data/products'); //my mock data
const dotenv = require('dotenv');
// eslint-disable-next-line no-unused-vars
const colors = require('colors');
const dbConnect = require('./config/db');
dotenv.config();
dbConnect()
.then(() => {
// callback();
console.log('db connected');
})
.catch((err) => {
console.error(`Error: ${error.message}`.red.underline.bold);
process.exit(1);
});
const app = express();
app.get('/', (req, res) => {
res.send('app is running');
});
app.get('/api/products', (req, res) => {
res.json(products);
});
app.get('/api/products/:id', (req, res) => {
const product = products.find((p) => p._id === req.params.id);
res.json(product);
});
const PORT = process.env.port || 5000;
const server = app.listen(PORT,
// eslint-disable-next-line max-len
console.log(`server running in ${process.env.NODE_ENV} on port ${PORT}`.yellow.bold));
process.on('unhandledRejection', (err) => {
console.log('err', err.name, err.message);
console.log('unhadled rejection');
server.close(() => {
process.exit(1);
});
});
As you see, nothing too fancy here, yes, I understand that my port isn't killed on restart but I don't understand that behaviour and how to prevent it. P.S. I'm using windows.
Upvotes: 0
Views: 1359
Reputation: 76
This error raised because any application running on port 5000. To fix it you just need to kill the running application on port 5000. Follow the steps below:
Step 0: stop your application If you are currently trying to run it on port 5000
Step 1: run command prompt as administrator
Step 2: run
-->" netstat -ano | findstr :5000
" <--
command to find the running application's pid number.
Note: In my case it is 4208
Step 4: run taskkill /PID {pid number} /F
to kill the running process.
Now you can run your app on port 5000
Update : Got a shortcut to kill the process
Just run TASKKILL /IM node.exe /F Then start your application :-)
Upvotes: 1