Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js错误";在抛出';std::bad#u alloc';what():std::bad“u alloc”;_Javascript_C++_Node.js - Fatal编程技术网

Javascript Node.js错误";在抛出';std::bad#u alloc';what():std::bad“u alloc”;

Javascript Node.js错误";在抛出';std::bad#u alloc';what():std::bad“u alloc”;,javascript,c++,node.js,Javascript,C++,Node.js,我正在digital ocean上使用node.js,并尝试运行文件上传/下载服务器 为了确保服务器在后台运行,并且不会在出现错误时退出,我使用以下方法 nohup nodejs server.js& 我使用的是nodejs而不是node命令,因为这是digital ocean的建议。 此服务器几乎是唯一用于上载和下载文件的服务器。这对大约两个文件有效,但随后服务器崩溃并出现以下错误: “在抛出'std::bad\u alloc'what():std::bad\u alloc'实例后终止调用”

我正在digital ocean上使用node.js,并尝试运行文件上传/下载服务器

为了确保服务器在后台运行,并且不会在出现错误时退出,我使用以下方法

nohup nodejs server.js&

我使用的是
nodejs
而不是
node
命令,因为这是digital ocean的建议。
此服务器几乎是唯一用于上载和下载文件的服务器。这对大约两个文件有效,但随后服务器崩溃并出现以下错误:

“在抛出'std::bad\u alloc'what():std::bad\u alloc'实例后终止调用”

我不知道是什么原因造成的,我希望能得到任何帮助。防止崩溃将非常好,但同时使节点服务器不会崩溃也非常好。我以为那就是诺胡普所做的,但显然不是。(我也无法永远正常工作)

以下是我的服务器的代码:

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);
var http=require('http'),
url=require('url'),
util=require('util'),
path=require('path'),
fs=需要('fs'),
qs=要求(“查询字符串”);
var nervouse=需要(‘nervouse’),
mime=require('mime');
var帐户={username:'test',password:'etc'};
var账户=[账户],
端口=9090,
函数目录树(文件名){
var stats=fs.lstatSync(文件名),
信息={
名称:path.basename(文件名),
路径:ip+':'+port+'/uploads/finished/'+path.basename(文件名),
类型:mime.lookup(文件名).substring(0,5)
};
if(stats.isDirectory()){
info.type=“文件夹”;
info.children=fs.readdirSync(文件名).map(函数(子){
返回目录树(文件名+'/'+子目录);
});
}
退货信息;
}
createServer(函数(请求、响应){
if(request.method.toLowerCase()=='get'){
var filePath='./content'+request.url;
如果(文件路径=='./content/')){
filePath='./content/home.html';
}
if(文件路径=='。/content/feed'){
a=目录树('./内容/上传/完成');
end(JSON.stringify(a));
}
var extname=path.extname(filePath);
var contentType=mime.lookup(extname);
存在(文件路径,函数(存在){
如果(存在){
fs.readFile(文件路径、函数(错误、内容){
如果(错误){
书面答复(500);
response.end();
}
否则{
writeHead(200,{'Content-Type':contentType});
响应。结束(内容为“utf-8”);
}
})
}否则{
书面答复(404);
response.end();
}
});
}
if(request.method.toLowerCase()=='post'){
var形式=新的收入形式;
如果(request.url='/verify'){
解析(请求、函数(错误、字段、文件){
对于(i=0;i
看起来您的脚本的可用内存刚刚用完

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


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

nodejs-v怎么说?