Giter Club home page Giter Club logo

Comments (5)

ralyodio avatar ralyodio commented on May 7, 2024 1
process.on('uncaughtException', function(err) {
    if(err.errno === 'EADDRINUSE')
         console.log(...);
    else
         console.log(err);
    process.exit(1);
});  

from discussions.

christianbundy avatar christianbundy commented on May 7, 2024

Thanks, I hadn't considered that! 🤦‍♂️ Is this something that you think Koa should be throwing or is uncaughtException the expected error behavior?

from discussions.

adjenks avatar adjenks commented on May 7, 2024

When you call listen() Koa creates a server on the fly using the http module, seen here: https://github.com/koajs/koa/blob/422e539e8989e65ba43ecc39ddbaa3c4f755d465/lib/application.js#L77-L81
According to the node documentation, you can handle that error like this:

server.on('error', (e) => {
  if (e.code === 'EADDRINUSE') {
    console.log('Address in use, retrying...');
    setTimeout(() => {
      server.close();
      server.listen(PORT, HOST);
    }, 1000);
  }
});

But since Koa calls listen before you can touch the server object, you can't. You'll have to make the server yourself like so:

const server = http.createServer(yourKoaApp.callback())
/* -- insert error handling code from above -- */
server.listen(portNumberOrWhatever)

I think if this were to be fixed in Koa, it could be done a variety of ways, but the most sensible to me would be to just depreciate the listen function because it doesn't really do much except save a line of code and obscure this error in the process.

As per the docs it's sugar: https://github.com/koajs/koa/blob/master/docs/api/index.md#applisten

from discussions.

jdmarshall avatar jdmarshall commented on May 7, 2024

Express has essentially the same bootstrap code, and has the same problem.

Port in use triggers an uncaught error instead of throwing from listen(), which means the 'problem' is in http.

from discussions.

kyr0 avatar kyr0 commented on May 7, 2024

Yeah, bad API design :/

from discussions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.