I'm running a nodejs web app with express/body-parser and I've been having trouble in the last few weeks making POST requests. I get the following error within seconds of making a request:
BadRequestError: request aborted at IncomingMessage.onAborted (/home/mercha67/solida-server/node_modules/raw-body/index.js:231:10)
at IncomingMessage.emit (node:events:369:20)
at abortIncoming (node:_http_server:595:9)
at socketOnClose (node:_http_server:586:3)
at Socket.emit (node:events:381:22)
at TCP.<anonymous> (node:net:665:12)
This doesn't happen on my local server, only on my production server.
According to body-parser documentation, "This error will occur when the request is aborted by the client before reading the body has finished. The received property will be set to the number of bytes received before the request was aborted and the expected property is set to the number of expected bytes. The status property is set to 400 and type property is set to 'request.aborted'."
So it seems that TCP socket is closing before the server can finish reading the request. From what I could gather, that doesn't mean the request is too large and indeed doesn't seem to be the case here.
This could be an issue with the device sending the request as it always happens on my laptop (on every browser), but doesn't happen when I send POST requests to the app through my mobile phone. At this point I'm unsure if other people using the app are also experiencing this issue, but maybe it's just me. What could cause that?
This is my code:
'use strict';
const express = require('express');
const http = require('http');
const { session } = require('./middleware/session');
const router = require('./routes/index');
const app = express();
app.use(session);
app.use(express.json({limit: '10mb'}));
app.use(express.urlencoded({extended: false, limit: '10mb'}));
const server = http.createServer(app);
const PORT = 5000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
app.use(router);
I've tried switching between bodyParser.json()/bodyParser.urlencoded() and express.json()/express.urlencoded(), but that doesn't seem to make a difference.
This was working fine until recently and I can't think of what could have changed to cause this error.
Any ideas on how to solve this? Thanks!
UPDATE 03/22/2022
With some help from a friend, I managed to find out that my antivirus software, Bitdefender, was the culprit. Everything works fine when I disable Bitdefender. I still have no clue, however, as to what in the code could be causing Bitdefender to abort POST requests sent to my server. If anyone has experienced this before, any help would be greatly appreciated.