You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// using nodejs we can write code to retry to listen on port usingconstserver=http.createServer().listen(3000);server.on('error',function(err){if(err.code==='EADDRINUSE'){console.log('Address in use, retrying...');setTimeout(()=>{server.close();server.listen(3000,'localhost');},1000);}});
how to do this same thing in express
constapp=require('express')();app.on('err',function(err){console.log('error occured');// this doesnt work});
It would be the same way you do with Node.js, as app.listen() returns the node.js server object. You can also provide app as the first argument to http.createServer().
this is request handler only and then whole code looks like
constapp=require('express')();constserver=http.createServer(app);server.listen(3000);server.on('error',function(err){if(err.code==='EADDRINUSE'){console.log('Address in use, retrying...');setTimeout(()=>{server.close();server.listen(3000,'localhost');},1000);}});
Activity
dougwilson commentedon Feb 4, 2022
It would be the same way you do with Node.js, as
app.listen()returns the node.js server object. You can also provideappas the first argument tohttp.createServer().tarunsinghh commentedon Feb 4, 2022
didn't understand this part -> You can also provide app as the first argument to http.createServer(). , code please
dougwilson commentedon Feb 4, 2022
const server = http.createServer(app)tarunsinghh commentedon Feb 4, 2022
okay, this part
this is request handler only and then whole code looks like
Thanks.