Node.js error "terminate called after throwing an insta
Node.js error "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc"
我在数字海洋上使用node.js,并试图运行文件上传/下载服务器。
为了确保服务器在后台运行并且不会在错误时退出,我使用以下
nohup nodejs server.js &
我使用nodejs
而不是node
命令,因为这是数字海洋推荐的。
这个服务器几乎是专门用来上传和下载文件的。这对大约两个文件有效,但随后服务器崩溃并出现以下错误:
"terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc"
我不知道是什么原因造成的,我将感谢任何帮助。防止崩溃固然很好,但也要确保节点服务器不会崩溃。我认为这是nohup
所做的,但显然不是。(我也没能永远正常工作)。
var http = require('http'),
url = require('url'),
util = require('util'),
path = require('path'),
fs = require('fs'),
qs = require('querystring');
var formidable = require('formidable'),
mime = require('mime');
var account = {username: 'test', password: 'etc'};
var accounts = [account],
port = 9090,
function dirTree(filename) {
var stats = fs.lstatSync(filename),
info = {
name: path.basename(filename),
path: ip + ':' + port + '/uploads/finished/' + path.basename(filename),
type: mime.lookup(filename).substring(0, 5)
};
if (stats.isDirectory()) {
info.type = "folder";
info.children = fs.readdirSync(filename).map(function(child) {
return dirTree(filename + '/' + child);
});
}
return info;
}
http.createServer(function(request, response) {
if(request.method.toLowerCase() == 'get') {
var filePath = './content' + request.url;
if (filePath == './content/') {
filePath = './content/home.html';
}
if (filePath == './content/feed') {
a = dirTree('./content/uploads/finished');
response.end(JSON.stringify(a));
}
var extname = path.extname(filePath);
var contentType = mime.lookup(extname);
fs.exists(filePath, function (exists) {
if (exists) {
fs.readFile(filePath, function (error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, {'Content-Type': contentType});
response.end(content, 'utf-8');
}
})
} else {
response.writeHead(404);
response.end();
}
});
}
if (request.method.toLowerCase() == 'post') {
var form = new formidable.IncomingForm;
if (request.url == '/verify') {
form.parse(request, function (err, fields, files) {
for (i = 0; i < accounts.length; i++) {
if (fields.username == accounts[i].username && fields.password == accounts[i].password) {
fs.readFile('./content/uploadForm.html', function (error, content) {
if (error) {
response.end('There was an error');
} else {
response.end(content);
}
});
} else {
fs.readFile('./content/invalidLogin.html', function (error, content) {
if (error) {
response.end('There was an error');
} else {
response.end(content);
}
});
}
}
});
} else if (request.url == '/upload') {
var oldPath,
newPath,
fileName;
form.uploadDir = './content/uploads/temp/';
form.keepExtensions = true;
form.parse(request, function (err, fields, files) {
type = files['upload']['type'];
fileName = files['upload']['name'];
oldPath = files['upload']['path'];
newPath = './content/uploads/finished/' + fileName;
});
form.on('end', function () {
fs.rename(oldPath, newPath, function (err) {
if (err) {
response.end('There was an error with your request');
console.log('error')
} else {
response.end('<h1>Thanks for uploading ' + fileName + '<h1>');
}
});
});
}
}
}).listen(port);
console.log('listening on ' + port);
看起来您的脚本刚刚用完可用内存。
很可能你上传或下载了非常大的文件,并且在接收或发送时读取了内存中的完整文件。
你应该使用流操作重写你的代码,而不是逐个块地处理文件。
相关文章:
- missing ) after argument list DATEDIF
- SlideDown()不会'不能在jQuery中使用After()
- setTimeOut AFTER jQuery表单提交
- 插入后单击锚点标记上的事件after不起作用
- 在javascript中修改pseudo-select:after
- Activates javascript after 'x' seconds
- jQuery after() uncatch SyntaxError: 意外令牌 ILLEGAL
- Make:如果父元素溢出,after元素将扩展到全宽
- 为node.js中的方法设置before/after钩子
- wrap() 不能很好地与 after() 一起使用
- Select2 minimumInputLength Not Working After append()
- After Effects/ExtendScript Function.caller等效程序
- target string after second <br> jquery
- Call Javascript After Directive Renders DOM for ShareThis
- jQuery:如何选择所有具有:before或:after的伪元素
- SyntaxError: missing ) after argument list in fire bug
- javascript SyntaxError: missing ) after argument list
- .after()在angularjs(嵌套ng重复递归迭代)中
- Callback after $.get?
- Node.js error "terminate called after throwing an insta