Giter Site home page Giter Site logo

Comments (2)

dougwilson avatar dougwilson commented on May 5, 2024 1

Check the README... https://github.com/expressjs/timeout#express-3x and the multiple examples.

from timeout.

mikeuduc avatar mikeuduc commented on May 5, 2024 1

Edit: having issues with my comment. Will update shortly...


@Omar1123 The problem is that connect-timeout will call next(err) if you do not pass {respond: false} in the options. This will cause the express router to skip all of your middleware(s) and to your error handler. - https://expressjs.com/en/guide/error-handling.html

To me, the documentation seems outdated (express v3) or incorrect because haltOnTimedout will never get called unless you pass {respond: false}, yet the example uses the default { respond: true } with haltOnTimedout as middleware.

Putting it together, you can either pass { respond: false } and create your own error to pass to your error handler as follows:

function haltOnTimedout(req, res, next){
	if (!req.timedout){
		next();
	}
	else{
		let err = new Error("My custom timeout error");
		err.status = 504;
		next(err);
	}
}

function errorHandler( function(err, req, res, next) {
        //handle your timeout error here
        res.sendStatus(err.status)
});

router.post( '/upload', connectTimeout('1s', { respond: false } ), multipartMiddleware, haltOnTimedout, successHandler, errorHandler)

Or you can use default {respond: true} which will skip your haltOnTimedout middleware and process the error from connect-timeout

router.post( '/upload', connectTimeout('1s'), multipartMiddleware, successHandler, timedoutErrorHandler)

from timeout.

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.