How to save a CSV file directly to a folder destination?
Currently my code prompts the save window in the browser, I DO NOT want that.
I want NodeJS to save the CSV directly to a folder destination
How can I do that?
var fs = require('fs');
var express = require('express');
csv = require('express-csv');
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var app = express();
app.get('/scraper', function(req, res){
var offset = req.query.offset || 0;
var limit = req.query.limit || 50;
if(gCounter === 0){
getStreamerInfo(offset, limit, function(response){
console.log("FILE READY!");
res.csv(response); // this export to browser but I want to save directly to a folder
});
}
});
Jus in case someone is looking for the same answer, this is what I did:
var fs = require('fs');
var jsonexport = require('jsonexport');
jsonexport(response,function(err, csv){
fs.writeFile("C:/outcome/c.csv", csv, function(err) {
if(err) {}
});
});
//res.csv(response);
Related
I am creating a sample project with NodeJs and jsPDF. When I run, it echos ReferenceError: window is not defined. I also used John Gordon answer from here, but again also same problem.
I tried with
var express = require('express');
var jsPDF = require('jspdf');
var app = express();
app.get('/', function(req, res)
{
global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
var fs = require('fs');
var jsPDF = require('jspdf');
var jsPDFTable = require('jspdf-autotable');
var doc = new jsPDF();
doc.text("Hello", 10, 10);
var data = doc.output();
fs.writeFileSync('./tmp/storage/pdf/document.pdf', data);
delete global.window;
delete global.navigator;
delete global.btoa;
});
var port = process.env.PORT || 8080;
app.listen(port);
console.log('Server started');
module.exports = app;
All you need to do is to remove your var jsPDF = require('jspdf'); at the top and to have the similar declaration inside your app.get.. (which you already have) function like this,
var express = require('express');
var app = express();
app.get('/', function(req, res)
{
global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
var fs = require('fs');
var jsPDF = require('jspdf');
var jsPDFTable = require('jspdf-autotable');
var doc = new jsPDF();
doc.text("Hello", 10, 10);
var data = doc.output();
fs.writeFileSync('./document.pdf', data);
delete global.window;
delete global.navigator;
delete global.btoa;
});
var port = process.env.PORT || 8080;
app.listen(port);
console.log('Server started');
module.exports = app;
Hope this helps!
The specified package jspdf is a client only library and need to be used in a browser environment to work properly.
The description is clear in the package home page:
A library to generate PDFs in client-side JavaScript.
Now, the reason an npm package is available is because bundlers like Webpack and Browserify can load npm packages and convert them into a proper browser compatible script. The require() is not defined in a browser environment and will not work without these bundlers.
So, either choose a library that supports NodeJS like
https://www.npmjs.com/package/pdfkit or shift your PDF related code to browser and work with it.
EDIT:
https://github.com/MrRio/jsPDF/issues/566#issuecomment-382039316
shows that you can use the library in NodeJS env by making the following changes.
In that case, you need to define the global variables before requireing the module.
global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
var fs = require('fs');
var jsPDF = require('jspdf');
var jsPDFTable = require('jspdf-autotable');
var app = require('express')();
app.get('/', function(req, res)
{
var doc = new jsPDF();
// ...
}
You can place the script tag either in the head or body tag in the html page, either one is fine. To decide where to place, this answer might be of help
I wish to read a file from a url and create a download stream with a different file name using nodejs on lambda.
Currently I am trying but failing with this code.
var fs= require('fs');
var url="https://upload.wikimedia.org/wikipedia/commons/5/51/Google.png";
fs.rename(url, "download.png", function(err) {
if ( err ) console.log('ERROR: ' + err);
});
fs.rename should be used for renaming the local file.
In your case, you would like to download a file from external URL and save it to new name, you can try this solution instead
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("download.png");
var request = http.get("https://upload.wikimedia.org/wikipedia/commons/5/51/Google.png", function(response) {
response.pipe(file);
});
I'm in the process of making my own Discord bot and I have no previous experience with JS. I'm making a feature that downloads and posts an image from this website: http://random.dog
I have successfully downloaded images from elsewhere when I know the exact link to the picture. Here's the relevant part of my code:
var download = function(uri, filename, callback){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);}
download('http://random.dog/14769-27888-18622.jpg', 'dog.png',
I'm using this request module https://www.npmjs.com/package/reques , is it possible to get images without the exact URL, with that module?
this who you can fetch URL dynamicaly and download picture with name you need these two module request-promise, cheerio,
var app = require('express')();
var http = require('http').Server(app);
fs = require('fs');
var fs = require('fs'),
request = require('request');
//save image
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
//get URL
var Imgpath="";
var rp = require('request-promise'),
cheerio = require('cheerio'),
url = require('url'),
base = 'http://random.dog/';
var options = {
uri: base,
method: 'GET',
resolveWithFullResponse: true
};
rp(options)
.then (function (response) {
$ = cheerio.load(response.body);
var relativeLinks = $("img");
relativeLinks.each( function() {
var link = $(this).attr('src');
var fullImagePath = url.resolve(base, link); // should be absolute
//pass to download to download image link is file name and base is base url
download(base+link,link,function()
{
console.log("wao great we done this...THINK DIFFERENT")
})
});
});
app.listen(3000);
console.log("server start");
app.get('/',function(req,res){
res.sendfile(__dirname + '/a.html');
});
I am trying to zip the file and download from server using easy-zip module. But now I can write into a server using following code but how can i make it downloadable???
var app = require('express')();
var easyzip = require('easy-zip');
app.get('/api/downloadFile',function(req,res){
console.log("inside req");
var data = "<html><body><h1>Inside new Html</h1></body></html>";
var zip2 = new easyzip.EasyZip();
var jsFolder = zip2.folder('data');
jsFolder.file('app.js','alert("hello world")');
jsFolder.file('index.html',data);
zip2.writeToFile('folder.zip');
});
Try zip2.writeToResponse(res,'folder.zip'); instead of zip2.writeToFile('folder.zip');
Instead of write into a file write into a response
var app = require('express')();
var easyzip = require('easy-zip');
app.get('/api/downloadFile',function(req,res){
console.log("inside req");
var data = "<html><body><h1>Inside new Html</h1></body></html>";
var zip2 = new easyzip.EasyZip();
var jsFolder = zip2.folder('data');
jsFolder.file('app.js','alert("hello world")');
jsFolder.file('index.html',data);
zip2.writeToResponse(res,'folder');
res.end();
})
I have a file in D: Drive of my local system. I need to download the file into the E: Drive. How to do this using node.js and http request? I am a beginner in node.js. Please give me valuable suggestions.
Note: The file may be in any type.
Here is an example:
// upload.js
var fs = require('fs')
var newPath = "E:\\newPath";
var oldPath = "D:\\oldPath";
exports.uploadFile = function (req, res) {
fs.readFile(oldPath, function(err, data) {
fs.writeFile(newPath, data, function(err) {
fs.unlink(oldPath, function(){
if(err) throw err;
res.send("File uploaded to: " + newPath);
});
});
});
};
// app.js
var express = require('express'), // fetch express js library
upload = require('./upload'); // fetch upload.js you have just written
var app = express();
app.get('/upload', upload.uploadFile);
app.listen(3000);
Basically there are two parts, one doing the copying from one drive to another, and the other one is for triggering. Once you run you app.js and make a GET request to localhost:3000/upload it will copy the file from newPath to the oldPath. For further information have a look to expressjs and fs.
Assuming it's a text file, you would have to write two node.js server.
The first would answer (all/specific, your choice) http get with the content of the file, the other would make a get and download the file.
server.js: Will work only for text file
var http = require('http'),
fs = require('fs'),
server = http.createServer(function (req, res){
res.writeHead(200, {'content-type': 'text/text'});
fs.readFile('E:/path/to/file.txt', function (data) {
res.write('' + data);
res.end();
});
}).listen(8080);
client.js
var http = require('http'),
fs = require('fs'),
file = fs.createWriteStream('D:/path/to/new.txt', {flags: 'w'});
http.get('http://localhost:8080', function (res) {
res.pipe(file, {end: 'false'});
res.on('end', function() {
file.end();
});
});
EDIT:
The only advantage versus anvarik's solution is that I don t use express...