I'm just approaching node.js
, so I'm running the obvious example - in a file called server.js :
var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("it's alive!");
response.end();
}
http.createServer(onRequest).listen(8888);
I run this from terminal with node server.js
and all it's ok, server responds.
But when I need to change the server.js :
node server.js
;server responds me with the following error:
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8888
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Object.<anonymous> (/var/www/vhosts/lizardhq.com/httpdocs/server.js:9:30)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
what I'm doing wrong? Many thanks.
The error you encounter means your server was not properly shutdown, or is still shutting down, and the port he's listening on is still in use.
You could wait a few minutes for the port to become available, or, if you are using linux, you could find the process id ( PID
) that is using the port via lsof | grep 8888
lsof | grep 8888
which will give you a PID in the second column, and then kill it via kill -9 PID
.
You could also use supervisor
and npm
to watch for change to your file system, and restart your server automagically:
Install supervisor
:
npm install supervisor -g
Then, in your package.json
file, add this line:
"scripts": {
"start": "supervisor -w 'server.js' node server.js"
},
Now, start your server using the following command:
npm start
Now, every time you save the server.js
file, your server will restart automaticaly. No more EADDRINUSE
error !
This error means you've already got a Node.js server (or some other server) running on port 8888
. You need to STOP your other process which is using that port before you can start your Node server on the same port =)
Only one program can bind to a port at a time.
sometimes it happens to me, open the task manager | ps -a depending system and kill node process
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.