express-slow-down: On v1.4.1 when slow delay is triggered the http request end up hanging up indefinitly and never being resolved

On v1.4.1 when slow delay is triggered the http request end up hanging up indefinitly and never being resolved

I’m on Windows with NodeJS v16.15.0

From what i saw the issue is that on this line we are sending the req object and with this object being sent the on-finished library trigger immediatly the onFinished event because the req.complete is already set (complete on req mean that the parsing of the request is done but not the http context being resolved source )

There are 2 possible solutions :

  • Using the res object instead of the req one on the line linked above
  • Not using on-finished library at all and replacing line linked above with req.on('close', () => {

I personnaly prefer the second solution because it means One less dependency to have so much cleaner to me …

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (12 by maintainers)

Most upvoted comments

working perfectly !! Nice work !!

@ntedgi @nfriedly @danursin I finally was able to reproduce the bug :

Apparently its the body-parser causing the “issue” because it reads the request body and this was triggering the request on-finished event, which is perfectly normal (thats the whole point of request on-finished).

Steps to reproduce

npm i express express-slow-down body-parser

const express = require("express");
const slowDown = require('express-slow-down');
const bodyParser = require('body-parser')

const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' }))

// parse application/json
app.use(bodyParser.json({ limit: '50mb'}))

app.use(slowDown({
  windowMs: 60000,
  delayAfter: 5,
  delayMs: 1000, 
  maxDelayMs: 10000, 
}));

// All controllers should live here
app.post("/", function rootHandler(req, res) {
  res.end("Hello world!");
});
app.listen(3000);
curl --request POST \
  --url http://localhost:3000/ \
  --header 'Content-Type: application/json' \
  --data '{
  "test": "thisistestjson"
}'

How to fix :

Refer to my 2 solutions on the first post of this issue for possible fix solutions.

Maybe “real world” isn’t the best description; I was thinking of something that still runs inside of jest (so it would run on node 14,16, and 18 on windows, mac, and linux in CI) - but it would use an actual HTTP server and actual HTTP requests, both in node.js - possibly in the same process, possibly in separate processes.