简体   繁体   English

如何解决node.js中的内存问题?

[英]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.

相关问题 如何修复此node.js路径问题? - How can I fix this node.js path issue? 如何解决Node.js中的此回调错误? - How can i fix this call back error in Node.js? 尝试在 Heroku 上托管我的 rest api (node.js) 时如何解决此问题? - How do I fix this problem when trying to host my rest api (node.js) on Heroku? 如何解决 node.js “JavaScript heap out of memory”问题? - How to solve node.js “JavaScript heap out of memory” problem? 如何解码 node.js 中 memory 数据的含义并调试 memory 泄漏? - How can I decode the meaning of memory data in node.js & debug the memory leak? 如何修复此“缺少模块”错误(node.js/discord.js)? - How can I fix this "missing module" error (node.js/discord.js)? 在 Node.js 中运行 Mapkit JS 时如何修复“窗口未定义”(Firebase 函数) - How can I fix 'window is undefined' when running Mapkit JS in Node.js (Firebase Functions) 如何限制节点侧(而不是操作系统侧)的node.js中的内存使用? - How can I limit memory usage in node.js on the node-side (not OS side)? 我该如何解决这个问题(node.js,discord.js) - How can I resolve this problem (node.js, discord.js) 我可以将关系表存储在内存中并在 node.js 中查询吗? - Can I store a relational table in memory and query it in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM