Unhandled error event using node.js

0

I'm writing a script for node.js:

obj={}
require("socket.io").listen(
require("http").createServer((req,res)=>{
res.writeHead(200,{"content-type":"text/html"})
res.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>')
res.write('<script>io().on("msg",(e)=>{eval(e.script)})</script>')
res.end()
}).listen("8888",()=>{console.log("Здравствуйте?")})
).on("connection",(w)=>{
w.i=Math.random()
obj[w.i]={x:0,y:0,xs:1,ys:0}
w.emit("msg",{script:'k=[]'})
w.emit("msg",{script:'document.body.style.margin="0"'})
w.emit("msg",{script:'c=document.createElement("canvas")'})
w.emit("msg",{script:'document.body.appendChild(c)'})
w.emit("msg",{script:'ctx=c.getContext("2d")'})
w.emit("msg",{script:'onkeydown=onkeyup=(e)=>{k[e.keyCode]=e.type=="keydown"}'})
w.emit("msg",{script:'setInterval(()=>{for(i=0;i<400;i++){if(k[i]){io().emit("key",{code:i})}}}'})
w.on("key",(e)=>{
console.log(e.code+" from "+w.i)
if(e.code==37){obj[w.i].xs-=0.1}
if(e.code==38){obj[w.i].ys-=0.1}
if(e.code==39){obj[w.i].xs+=0.1}
if(e.code==40){obj[w.i].ys+=0.1}
})
obj[w.i].ys+=0.01
setInterval(()=>{
w.emit("msg",{script:'c.width=innerWidth'})
w.emit("msg",{script:'c.height=innerHeight'})
for(i in obj){
w.emit("msg",{script:'ctx.fillRect('+obj[i].x+','+obj[i].y+',32,32)'})
}},25)
})
setInterval(()=>{
for(i in obj){
obj[i].x+=4
obj[i].y+=4
}},25)

I tried to run it with Node in a Terminal (I have Lubuntu 17.04 ), and I had that error:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8888
    at Object.exports._errnoException (util.js:907:11)
    at exports._exceptionWithHostPort (util.js:930:20)
    at Server._listen2 (net.js:1250:14)
    at listen (net.js:1286:10)
    at Server.listen (net.js:1382:5)
    at Object.<anonymous> (/home/killian/index.js:8:4)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

I and a friend of mine tried to correct it, but nothing worked. The strange thing is that he did not have this problem. Can anyone help us?

Thanks in advance.

    
asked by KCJV 02.09.2017 в 19:17
source

1 answer

0

That's because the port where you want to start the server is in use. Use another port in the

part
.listen("8888",()=>{console.log("Здравствуйте?")})

in listen, the first parameter is the port, change it to another one.

Commonly happens when you have another node script running or another program or application is using it. Put value for example, 8080, 3000, etc.

    
answered by 02.09.2017 / 23:26
source