我构建了一个 Web 应用程序,用户上传 2 个由整数值组成的 JSON 文件,并将前端发布到服务器(使用 Multer),其中这 2 个 JSON 文件相互比较,以计算中值的差异和交集每个 JSON 文件。
它适用于小型 JSON 文件,但是当我尝试上传具有大量值(大约 100k)的大型 JSON 文件时,它会抛出 ERR_CONNECTION_RESET。
前端,简单的HTML:
<form action="http://localhost:3000/api/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept=".json" multiple /><br />
<input type="submit" value="Submit">
</form>
后端:
服务器.js/app.js:
const express = require("express");
const app = express();
const router = require("./routes/routes");
app.use("/api/upload", router);
app.listen(3000);
routes.js:
const express = require("express");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const router = express.Router();
let oneValues = new Array(); // array to store the values of file 1
let twoValues = new Array(); // array to store the values of file 2
// Multer storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads");
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage });
// main router and processing logic
router.post("/", upload.array("file"), (req, res, next) => {
const one = require("../uploads/1.json"); // accessing uploaded file 1
const two = require("../uploads/2.json"); // accessing uploaded file 2
// iterating the file 1 and storing the values in oneValues array
for (let i = 0; i < one.length; i++) {
oneValues[i] = one[i].value;
}
// iterating the file 2 and storing the values in twoValues array
for (let i = 0; i < two.length; i++) {
twoValues[i] = two[i].value;
}
// 3 logics to find the difference and intersection
let differenceOne = oneValues.filter((x) => !twoValues.includes(x));
let differenceTwo = twoValues.filter((x) => !oneValues.includes(x));
let intersection = oneValues.filter((x) => twoValues.includes(x));
// sending the results back to front-end
res.json({ differenceOne, differenceTwo, intersection });
});
module.exports = router;
数据集: 这些是非常小的 JSON 文件,可以正常工作
1.json:
[
{
"value": 1
},
{
"value": 2
},
{
"value": 3
},
{
"value": 4
},
{
"value": 5
}
]
[
{
"value": 1
},
{
"value": 2
},
{
"value": 6
},
{
"value": 7
}
]
这是包含大约 100k 值的大型 JSON 文件: https://file.io/rQZfpnPENtfR
谢谢!
您可以尝试以下其中一项:
答:
var server = app.listen(300);
server.setTimeout(500000);
乙:
app.use(express.limit('100M'));
C:
var upload = multer({ storage : storage2, limits: { fileSize: 1024 * 1024 * 50 } });