我在节点中有一个有角度的应用程序,该应用程序使用express来路由各种不同的http请求。当多个用户尝试同时使用该应用上传文件时,我首先遇到了这个问题。我首先以为这是全局范围变量的问题,该变量重置了重叠的异步/等待请求的数据。
但是,我能够通过卷曲到本地主机上的相同端口来复制此问题(“无法加载资源:服务器以400状态响应”),这是获取请求的默认路由。我以为Express应该可以处理多个HTTP请求?这是我在app.js中的中间件代码:
const PORT = 9001;
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const routes = require('./routes/routing');
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT, PATCH, DELETE");
next();
});
app.use(bodyParser.json({limit: '20mb', extended: true}))
app.use(bodyParser.urlencoded({limit: '20mb', extended: true}))
routes.appRoutes(app);
app.get('/', function (req, res) {
const example = [];
for (i = 0; i < 1000; i++) {
for (j = 0; j < 1000; j++) {
example.push("test2");
}
example.push("test");
}
res.send(example);
});
app.all('*', function(req, res, next){
res.send('Page not found.');
});
app.listen(PORT, () => {
console.log("App is listening under the port: " + PORT);
});
这是我的routing.js:
const path = require("path");
const multer = require('multer');
const { document } = require('./../controllers');
const diskStorage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, path.resolve("./server/uploads"))
},
filename: function (req, file, callback) {
callback(null, file.originalname + '-' + Date.now() + ".csv")
}
});
const uploadConfig = multer({
storage: diskStorage,
});
const appRoutes = (app) => {
app.post('/upload', uploadConfig.any(), document.upload);
app.post('/process', document.process);
app.get('/getFiles', document.getFiles);
app.get('/getBatches/:fileId', document.getBatches);
};
module.exports = {
appRoutes
}