expressjs / timeout

Request timeout middleware for Connect/Express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

req.timedout always come out to be false irrespective of timeout happens or not

aryankanwar opened this issue · comments

I am using connect-timeout to handle timeout for route.While this works correct but for the condition req.timedout this always come out out to be false irrespective of timeout or not. req.timedout always come out to be false irrespective of timeout happens or not.

[var express = require('express')
var bodyParser = require('body-parser')
var timeout = require('connect-timeout')

var app = express()
app.post('/save', timeout('1s'), bodyParser.json(), haltOnTimedout, function (req, res, next) {
  savePost(req.body, function (err, id) {
    if (err) return next(err)
    if (req.timedout) return
    res.send('saved as id ' + id)
  })
})

function haltOnTimedout (req, res, next) {
  console.log(req.timedout, "==req.timedout");
  if (!req.timedout) next()
}

function savePost (post, cb) {
  setTimeout(function () {
    cb(null, ((Math.random() * 40000) >>> 0))
  }, (Math.random() * 7000) >>> 0)
}
]

Screenshot 2020-07-16 at 12 23 06 PM

Screenshot 2020-07-16 at 12 23 23 PM

This is because the timeout happens after you console.log runs. If you want to see it true, you need to log it after the tineout occurs. Based on your code, that would likely happen within your savePost callback.

@dougwilson even if you increase the timeout time still it comes out to be false.
if (!req.timedout) {
next()
}

This next() is always called irrespective of timeout happened or not.

I tried your code and I am getting true just fine if you move the log to after the timeout occurs. If you believe there us a bug in the code, though, you are welcome to submit a pull request with the appropriate fix.

Can you post the code so that I can check because I moved the log after timeout and still it shows false to me.
@dougwilson

If you want to see it true, you need to log it after the tineout occurs. Based on your code, that would likely happen within your savePost callback.

var express = require('express')
var bodyParser = require('body-parser')
var timeout = require('connect-timeout')

var app = express()
app.post('/save', timeout('1s'), bodyParser.json(), haltOnTimedout, function (req, res, next) {
  savePost(req.body, function (err, id) {
    if (err) return next(err)
    console.log(req.timedout, "==req.timedout");
    if (req.timedout) return
    res.send('saved as id ' + id)
  })
})

function haltOnTimedout (req, res, next) {
  if (!req.timedout) next()
}

function savePost (post, cb) {
  setTimeout(function () {
    cb(null, ((Math.random() * 40000) >>> 0))
  }, (Math.random() * 7000) >>> 0)
}

When using it at middleware level
In haltOnTimedout how will we handle because this is the part of our middleware where we can handle the flow of application, otherwise according to your code we will have to check req.timedout at each route level.
@dougwilson

It is only true after the timeout has occurred.

If you believe there is a bug in the code, though, you are welcome to submit a pull request with the appropriate fix.

How will we handle then at middleware level ?

@dougwilson
As we are checking here inside haltOnTimedout , if req.timedout always comes out to be false then next() is always called irrespective of timeout happens or not.

var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
var express = require('express')
var timeout = require('connect-timeout')
 
// example of using this top-level; note the use of haltOnTimedout
// after every middleware; it will stop the request flow on a timeout
var app = express()
app.use(timeout('5s'))
app.use(bodyParser())
app.use(haltOnTimedout)
app.use(cookieParser())
app.use(haltOnTimedout)
 
// Add your routes here, etc.
 
function haltOnTimedout (req, res, next) {
  if (!req.timedout) next()
}
 
app.listen(3000)

If you believe there is a bug in the code, though, you are welcome to submit a pull request with the appropriate fix.