[英]How can I fix a memory problem in node.js?
I get the following error. 我收到以下错误。 As I checked, it seems that the json file is too large to run because of insufficient server memory.
正如我检查的那样,由于服务器内存不足,似乎json文件太大而无法运行。
How can we solve this problem? 我们如何解决这个问题? There is a thing called Node streaming, it's hard for me to understand.
有一个叫做Node流的东西,这让我很难理解。 Can you explain it more easily?
您能更轻松地解释它吗?
server.js server.js
const express = require('express');
const cors = require('cors');
const userJson = require('./user');
const locationJson = require('./location');
const API_PORT = process.env.PORT || 3002;
const app = express();
app.use(cors());
const router = express.Router();
router.get("/getUserData", (req, res) => {
return res.json(userJson);
});
router.get("/getLocationData", (req, res) => {
return res.json(locationJson);
});
app.use("/api", router);
app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));
error messages 错误讯息
buffer.js:585
if (encoding === 'utf8') return buf.utf8Slice(start, end);
^
Error: Cannot create a string longer than 0x3fffffe7 characters
at stringSlice (buffer.js:585:43)
at Buffer.toString (buffer.js:655:10)
at Object.readFileSync (fs.js:392:41)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:816:22)
at Module.load (internal/modules/cjs/loader.js:666:32)
at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
at Function.Module._load (internal/modules/cjs/loader.js:598:3)
at Module.require (internal/modules/cjs/loader.js:705:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (/Users/k/Desktop/dev/backend/location.js:1:22)
You can stream the file and pipe() it directly into the res: 您可以流式传输文件并将其直接pipe()到res中:
const fs = require('fs');
const path = require('path');
const getBigJson = (req, res, next) => {
const pathToJson = path.join(__dirname, 'path/to/file');
const jsonStream = fs.createReadStream(pathToJson);
res.set({'Content-Type': 'application/json'});
jsonStream.pipe(res);
};
It should send it chunk by chunk without exhausting your memory. 它应该逐块发送它,而不会耗尽您的内存。
See https://github.com/substack/stream-handbook for a good explanation of streams. 参见https://github.com/substack/stream-handbook以获得关于流的良好解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.