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

时间:2019-06-17 09:53:39

标签: node.js json express

我收到以下错误。 正如我检查的那样,由于服务器内存不足,似乎json文件太大而无法运行。

我们如何解决这个问题? 有一个叫做Node流的东西,这让我很难理解。您能更轻松地解释它吗?

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}`));

错误消息

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)

1 个答案:

答案 0 :(得分:0)

您可以流式传输文件并将其直接通过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);
};

它应该逐块发送它,而不会耗尽您的内存。

有关流的详细说明,请参见https://github.com/substack/stream-handbook