-1

Here, I am doing some development and testing so each time I change something I've to exit server and do changes and start new server.

For first time when I run command node server.js file it works like a charm but when I exit it and do some changes and start again it gives me this error in console :

 throw er; // Unhandled 'error' event

I searched a bit about it and I found that this error comes when that port is already in used and when I changed port name and then start it works fine.

var express = require("express");
var app = express();
var port = 3702;

app.get("/", function(req, res){
    console.log(req);
    res.send("It works!");
});

app.on('error', function(err) {
    console.log(err);
});

app.listen(port);
console.log("Listening on port " + port);
Mohit Bumb
  • 2,327
  • 27
  • 49
  • It sounds like your server process is not actually exiting. How are you stopping the server process and on what platform are you running it? – jfriend00 Jul 18 '15 at 07:07
  • 10 hours go by and you can't respond to a simple question in a comment? We're trying to help you, but can't do that when you're non-responsive. There is insufficient information in your question as stated to know your problem. A properly terminated nodejs app does not keep the port occupied. – jfriend00 Jul 18 '15 at 17:03
  • Issue solved by Mohsen's answer http://stackoverflow.com/questions/5266152/how-to-exit-in-node-js – Mohit Bumb Jul 20 '15 at 05:28
  • Why did you fail to respond to any questions? The very first comment here asks you how you were exiting the program. And, because you failed to answer ANY questions asked here, nobody here could provide you an answer. I could have answered your question within minutes if you had merely responded back here. That's how this forum works best. You should not disappear and be silent. – jfriend00 Jul 20 '15 at 05:31
  • I dont work on weekends :P – Mohit Bumb Jul 20 '15 at 05:35
  • Then don't ask questions on weekends. You should only ask questions here when you can be responsive for at least the next 30-60 minutes to clarifying questions and then check back again a few hours later. It's a disservice to the site here and your questions will only get downvotes and/or closed if you aren't responsive. And, you will miss most of your opportunity to get an answer if your question is not cleared up in the first hour of posting (that's when most people see it). – jfriend00 Jul 20 '15 at 05:39
  • I'm explaining how this community works to someone who apparently does not understand it. We spend time here helping people who deserve to be helped and who respect the process that makes it efficient to be helped. Apparently, you don't care to be part of that community. – jfriend00 Jul 20 '15 at 05:42

1 Answers1

1

Changing the port number for every change you make to the server and starting a new process isn't good practice. From the way you phrased the question is seems you want to do this for development purposes to test changes as you go. There are tools, like nodemon, that help you do exactly this. They'll watch the files in your project and any change will trigger a restart of the server in the same port.

Using nodemon is as simple as installing it through npm: npm install -g nodemon and then start your application using nodemon instead of node.

dmlittle
  • 894
  • 6
  • 14