Node.js 尝试使用它下载大文件时应用崩溃
Node.js app crashing when trying to download large file using it
我正在尝试编写一个可以将 HTTP URL 转换为种子文件的程序。该项目主要用于下载 links 包含小文件的工作。就像一个 1-500Mb 的文件。我的问题是,如果文件大小超过应用程序崩溃或超时的大小。所以我想知道如何解决这个问题。下面是我的代码和 link 到 Github。
https://github.com/savadks95/FireBit
var http = require('http');
var webtorrentify = require('webtorrentify-link');
var fs = require('fs');
var url = require("url");
var path = require("path");
var validUrl = require('valid-url');
var express = require('express');
var getUrls = require('get-urls');
var remote = require('remote-file-size');
var app = express();
var downloadLink;
var fileName;
var fileSize;
var server;
var parsed;
var param;
var link;
var port;
port = process.env.PORT || 80;
app.get('/favicon.ico', function(req, res){
console.log('favicon request recived');
});
app.get('*', function(req, res){
if(req.url==='/'){
//app.use('/public/html', express.static(path.join(__dirname)));
fs.readFile('public/html/index.html', function (err, data) {
res.write(data);
});
}else if (req.url === '/l?thelink='){
fs.readFile('public/html/emptyRequest.html', function (err, data) {
res.write(data);
res.end();
});
}else{
//---------Reciving Url--------------------
console.log(req.query.thelink);
downloadLink=req.query.thelink;
//-----------------------------------------
//------------checking for valid url-------
if (validUrl.isUri(downloadLink)) {
console.log('Looks like an URL');
//-----------------------------------------
//----------Extracting filename-------------
parsed = url.parse(downloadLink);
fileName = path.basename(parsed.pathname);
console.log(path.basename(parsed.pathname));
//-------------------------------------------
//----------Finding File size----------------
remote(downloadLink, function(err, o) {
fileSize = (o/1024)/1024;
console.log('size of ' + fileName + ' = ' + fileSize+" MB");
//-------------------------------------------
if (fileSize < 501)
{
///////////////Creating Torrent////////////////////
webtorrentify(downloadLink)
.then(function (buffer) {
console.log('creating the torrent');
//res.send('what is');
//-------------------------------------------
res.setHeader('Content-Type', 'application/x-bittorrent');
res.setHeader('Content-Disposition', `inline; filename="${fileName}.torrent"`);
res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30 days
res.send(buffer);
console.log(fileName+'.torrent created');
res.end();
//-------------------------------------------
});
////////////////////////////////////////////////
}
else{
console.log('More than 500 MB');
res.send("<h4> More than 500 MB or invalid URL </h4>");
}
});
}
else {
console.log('not url');
fs.readFile('public/html/404.html', function (err, data) {
res.write(data);
res.end();
});
}
}
});
app.listen(port);
console.log('server up and running', port);
Node.js的纯文件(fs)和大数据处理功能通常无法处理超过1GB的文件,但只要多加一个NPM包EventStream,就可以解析一个庞大的数据集,而不会导致节点服务器崩溃。使用 EventStream 包,您可以下载最大 3GB 及以上的文件。
下面link给出了详细的实现。我想重申下面 link 中给出的示例实现解决了您的大文件下载问题。您将http url 转换为torrent 流的能力,您似乎已经很好地处理了它。
https://itnext.io/using-node-js-to-read-really-really-large-files-pt-1-d2057fe76b33
这里是事件流模块的 NPM 包。
https://www.npmjs.com/package/event-stream
希望对您有所帮助。
我正在尝试编写一个可以将 HTTP URL 转换为种子文件的程序。该项目主要用于下载 links 包含小文件的工作。就像一个 1-500Mb 的文件。我的问题是,如果文件大小超过应用程序崩溃或超时的大小。所以我想知道如何解决这个问题。下面是我的代码和 link 到 Github。
https://github.com/savadks95/FireBit
var http = require('http');
var webtorrentify = require('webtorrentify-link');
var fs = require('fs');
var url = require("url");
var path = require("path");
var validUrl = require('valid-url');
var express = require('express');
var getUrls = require('get-urls');
var remote = require('remote-file-size');
var app = express();
var downloadLink;
var fileName;
var fileSize;
var server;
var parsed;
var param;
var link;
var port;
port = process.env.PORT || 80;
app.get('/favicon.ico', function(req, res){
console.log('favicon request recived');
});
app.get('*', function(req, res){
if(req.url==='/'){
//app.use('/public/html', express.static(path.join(__dirname)));
fs.readFile('public/html/index.html', function (err, data) {
res.write(data);
});
}else if (req.url === '/l?thelink='){
fs.readFile('public/html/emptyRequest.html', function (err, data) {
res.write(data);
res.end();
});
}else{
//---------Reciving Url--------------------
console.log(req.query.thelink);
downloadLink=req.query.thelink;
//-----------------------------------------
//------------checking for valid url-------
if (validUrl.isUri(downloadLink)) {
console.log('Looks like an URL');
//-----------------------------------------
//----------Extracting filename-------------
parsed = url.parse(downloadLink);
fileName = path.basename(parsed.pathname);
console.log(path.basename(parsed.pathname));
//-------------------------------------------
//----------Finding File size----------------
remote(downloadLink, function(err, o) {
fileSize = (o/1024)/1024;
console.log('size of ' + fileName + ' = ' + fileSize+" MB");
//-------------------------------------------
if (fileSize < 501)
{
///////////////Creating Torrent////////////////////
webtorrentify(downloadLink)
.then(function (buffer) {
console.log('creating the torrent');
//res.send('what is');
//-------------------------------------------
res.setHeader('Content-Type', 'application/x-bittorrent');
res.setHeader('Content-Disposition', `inline; filename="${fileName}.torrent"`);
res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30 days
res.send(buffer);
console.log(fileName+'.torrent created');
res.end();
//-------------------------------------------
});
////////////////////////////////////////////////
}
else{
console.log('More than 500 MB');
res.send("<h4> More than 500 MB or invalid URL </h4>");
}
});
}
else {
console.log('not url');
fs.readFile('public/html/404.html', function (err, data) {
res.write(data);
res.end();
});
}
}
});
app.listen(port);
console.log('server up and running', port);
Node.js的纯文件(fs)和大数据处理功能通常无法处理超过1GB的文件,但只要多加一个NPM包EventStream,就可以解析一个庞大的数据集,而不会导致节点服务器崩溃。使用 EventStream 包,您可以下载最大 3GB 及以上的文件。
下面link给出了详细的实现。我想重申下面 link 中给出的示例实现解决了您的大文件下载问题。您将http url 转换为torrent 流的能力,您似乎已经很好地处理了它。
https://itnext.io/using-node-js-to-read-really-really-large-files-pt-1-d2057fe76b33
这里是事件流模块的 NPM 包。
https://www.npmjs.com/package/event-stream
希望对您有所帮助。