【发布时间】:2021-10-15 14:04:10
【问题描述】:
我正在对我创建的 node.js api 进行性能测试。
有一个 POST 端点接收图像作为 base64 中的输入。所以请求载荷很大,我的测试用例图像大约是 7-8mb。
当我同时对 10 个用户进行性能测试时,我收到以下错误:
请求:
{"log_id":"db0ac1d0-cafe-11e9-afec-e91ff7e323e3","level":"info","request":{"method":"POST","path":"<removed>","url":"<api_url_removed>","body":{},"forward_for":"<removed>","content_type":"application/json","content_length":"7687038"},"message":"Received","log_type":"request","timestamp":"2019-08-30 08:19:15"}
错误:
BadRequestError:请求中止 在 IncomingMessage.onAborted (/app/node_modules/raw-body/index.js:231:10) 在 IncomingMessage.emit (events.js:189:13) 在 abortIncoming (_http_server.js:449:9) 在 socketOnClose (_http_server.js:442:3) 在 Socket.emit (events.js:194:15) 在 TCP._handle.close (net.js:600:12)
我可以看到这些请求中有 10-20 个具有相同的错误。它们都有唯一的 ID。显然,由于我正在运行性能测试,因此发出这么多请求是正常的。
我的问题是为什么 node.js/express 会抛出这个 BadRequestError 以及我该如何处理它。
app.ts
class App {
public app: express.Application;
public config: any;
public log = Logger;
constructor() {
this.app = express();
this.errors();
this.environment();
this.database();
this.middleware();
this.routes();
}
private environment(): void {
this.config = new Config();
}
private errors(): void {
// TODO: Remove ( Handled By Winston );
// process.on("uncaughtException", (ex) => {
// process.exit(1);
// });
process.on("unhandledRejection", (ex) => {
throw ex;
});
}
private database(): void {
const uri: string = this.config.db.uri;
const options: mongoose.ConnectionOptions = this.config.db.options;
mongoose.connect(uri, options).then(
() => {
this.log.info("MongoDB Successfully Connected On: " + this.config.db.uri);
},
(err: any) => {
this.log.error("MongoDB Error:", err);
this.log.info("%s MongoDB connection error. Please make sure MongoDB is running.");
throw err;
},
);
}
private middleware(): void {
const logs = new LogMiddleware(this.app);
this.app.use(cors());
this.app.use(morgan("combined", {
skip: (req, res) => {
return (req.originalUrl === "/api/v2/healthcheck") ? true : false;
},
stream: {
write: (meta: any) => {
},
},
}));
this.app.use(express.json({limit: "16mb"}));
this.app.use(express.urlencoded({ limit: "16mb", extended: true, parameterLimit: 50000 }));
this.app.use(passport.initialize());
}
private routes(): void {
const routes = new Routes(this.app);
}
}
export default App;
【问题讨论】: