Node.js错误“抛出'std :: bad_alloc'what():std :: bad_alloc实例后调用终止”

Startec

我在数字海洋上使用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这样,但显然不是。(我也无法永远正常工作)。

这是我的服务器的代码:

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);
亚历克斯·奈特卡乔夫(Alex Netkachov)

看来您的脚本刚刚用完了可用内存。

您最有可能上传或下载非常大的文件,并且在接收或发送时读取了内存中的完整文件。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

std :: bad_alloc在内存位置0x002b123c

来自分类Dev

C ++ std :: bad_alloc错误

来自分类Dev

C ++ std :: bad_alloc错误

来自分类Dev

奇怪的std :: bad_alloc

来自分类Dev

当分配超出限制的对象时,Clang无法抛出std :: bad_alloc

来自分类Dev

异常:内存位置0x00e4df90上的std :: bad_alloc

来自分类Dev

已经创建的向量中的奇怪std :: bad_alloc

来自分类Dev

C ++奇怪的std :: bad_alloc异常

来自分类Dev

如果我得到std :: bad_alloc怎么办?

来自分类Dev

向量抛出bad_alloc

来自分类Dev

为什么在抛出'std :: bad_alloc'实例后终止调用?

来自分类Dev

将opencv :: Mat转换为std :: string会得到std :: bad_alloc

来自分类Dev

当我的训练数据很大时,为什么我会在抛出“ std :: bad_alloc实例”后收到“终止调用”?

来自分类Dev

大数据集的dijkstra计算期间的std :: bad_alloc

来自分类Dev

另一个std :: bad_alloc在内存位置

来自分类Dev

std :: bad_alloc在内存位置0x002b123c

来自分类Dev

带有QVector的bad_alloc但不带有std :: vector

来自分类Dev

C ++向量std :: bad_alloc错误

来自分类Dev

当分配超出限制的对象时,Clang无法引发std :: bad_alloc

来自分类Dev

为什么返回vector <string>会抛出std :: bad_alloc异常?

来自分类Dev

如果我得到std :: bad_alloc怎么办?

来自分类Dev

一段时间后获取std :: bad_alloc

来自分类Dev

错误更改/ usr /权限后的'std :: bad_alloc'

来自分类Dev

'new'导致std :: bad_alloc在相对不大的分配上

来自分类Dev

级联两个向量时的std :: bad_alloc

来自分类Dev

程序中的 C++ 向量 std::bad_alloc 错误

来自分类Dev

CPP - 未处理的异常 std::bad_alloc

来自分类Dev

抛出'std::bad_alloc'的实例后调用C++终止

来自分类Dev

如何修复“std::bad_alloc”

Related 相关文章

热门标签

归档