如何解决当 res.send() 大字符串时 Nodejs 服务器崩溃
我是 JS 和 NodeJs 的新手。当我发出 get 请求时,我的 Nodejs 应用程序崩溃,并试图返回一个大字符串
app.mjs
app.get('/log',function (req,res) {
....
function callback(data) {
res.status(200).json({ data })
}
logEndPoint = 'https://getmylogs/log'
deviceHandler.getLog(logEndPoint,callback); <--- get data from another API endpoint then return it using `res` as callback funcction
}
)
这里是 getLog()
函数
function getLog(url,callback,method='GET') {
var http = new XMLHttpRequest();
http.open(method,url,false);
...
http.onload = function() {
if (http.status != 200) {
callback(`Error ${http.status}: ${http.statusText}`);
} else { // show the result
// callback(http.responseText); <--- this crash the app
// callback('something short works derp');
console.log("sending the status back")
callback(Buffer.byteLength(http.responseText,"utf-8")); <----- print 1323196 bytes so 1.3 MB but sometimes it can be up to 5MB
return;
}
};
http.send();
}
知道为什么吗?
基本上我会在一个终端中使用 node --experimental-modules app.mjs
启动我的应用程序,而在另一个终端中我会运行 curl -G 'http://localhost:7000/log?param=param
解决方法
看来字符串的大小超过了vscode的堆内存配置。我建议使用以下命令运行 VSCode 以增加最大内存:
code --max-memory=12288mb
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。