Reputation: 1983
I know my question is repeated. Please read the question completely before marking as duplicate or anything.
I am learning node.js to create rest api & doing great on it.Everything was going smooth till i found this error( EADDRINUSE) on restart the app. This issue eat my precious hours but still not able to find the correct fix.
If the application is running for the 1st time, it launch's smoothly with out any port change for some reason we stop or restart the app this error message while be thrown from the server. Many of them suggested just change the port everything work smooth and I do agree with them.But When server is live & used worldwide you can't change the port to restart.
Adding Snippet of my Code.
app.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var config = require('./Config/config.js');
var api = require('./apis/api');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
app.all('/api/write/'+config.WRITE_API_VERSION+'/:apiName', function (req, res) {
api.handle.apply(null, ['write', req.method.toLowerCase(), req.params.apiName, req, res]);
console.log("in api end1"+req.body);
});
app.all('/api/read/'+config.READ_API_VERSION+'/:apiName', function (req, res) {
api.handle.apply(null, ['read', req.method.toLowerCase(), req.params.apiName, req, res]);
console.log("in api end2");
});
app.use(function (req, res, next) {
res.contentType('application/json');
next();
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(config.PORT);
console.log('Magic happens on port ' + config.PORT);
config.js
var configs = {
ENVIRONMENT: "development",
READ_API_VERSION: "v1.0",
WRITE_API_VERSION: "v1.0",
PORT: 8080
}
Object.freeze(configs);
module.exports = configs;
Package.json
"scripts": {
"start": "node app.js"
}
Error Message
node app.js
Magic happens on port 8080
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at EventEmitter.listen (/home/ubuntu/vcare_backend/VCare/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/home/ubuntu/vcare_backend/VCare/app.js:47:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
After running the command sudo netstat -plnt
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3277/node
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1073/sshd
tcp6 0 0 :::22 :::* LISTEN 1073/sshd
Upvotes: 2
Views: 591
Reputation: 11565
Node is already running on your machine.
In Terminal, find the running Node process with
ps -ax | grep node
This will return a list of the running node processes. The first column in the resulting list of processes is the process id (pid).
Kill the running node process with
kill -9 <pid>
This will kill the node process and free up the port for you to run your server.
Upvotes: 1
Reputation:
Your issue is that node is already running on port 8080. This is shown by the first line returned by the netstat/lsof command. Killing the process will allow you to restart the node process. You can do this on a mac by using the command
killall node
Where node is the name of the process.
Upvotes: 1