Node.js错误信息:"在抛出'std::bad_alloc'实例后终止调用,错误信息为:std::bad_alloc"

7

我正在使用Digital Ocean上的node.js,并尝试运行一个文件上传/下载服务器。

为了确保服务器在后台运行并且不会在错误时退出,我正在使用以下命令:

nohup nodejs server.js &

我使用nodejs而不是node命令,因为这是Digital Ocean推荐的。
该服务器几乎专门用于上传和下载文件。这对大约两个文件可行,但是然后服务器崩溃并显示以下错误:

"terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc"

我不知道是什么原因导致了这个错误,任何帮助都将不胜感激。防止崩溃将是很好的解决方案,使节点服务器不崩溃也将是很好的解决方案。 我认为这就是nohup的作用,但显然不是。(我也无法正确地让Forever工作)。

以下是我的服务器代码:

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

nodejs -v 是什么版本? - mscdex
1个回答

8

看起来你的脚本似乎只是因为可用内存不足而停止。

很可能是由于你上传或下载了非常大的文件,并在接收或发送时将整个文件读入内存。

建议您重写代码,使用流操作并逐块处理文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接