Piping a file results on a blank file - node.js

I have this piece of code in my nodejs app:
function (req, res) {
var serviceUrl = 'http://backend-server:8080/files/123';
require('request').get(serviceUrl).pipe(res);
}
If put http://backend-server:8080/files/123 on the browser it downloads a .pdf file. But when I go through nodejs, it also downloads a .pdf file, but the file is empty.
I can't understand why.
UPDATE: The problem was the connect-livereload middleware. Consider this a duplicate question for this question

Related

Nodsjs serve static files but change filename when downloading

I want to serve many files in a folder and thats possible with app.use('/files', express.static('uploads'));.
All my files have an uuid as suffix for example: 73694640-44e9-448d-bf2f-29148b59180b_myfile.txt.
Is there a short way to serve all files in this folder but when downloading them, the filename should be without the suffix/uuid?
Like http://localhost/files/73694640-44e9-448d-bf2f-29148b59180b_myfile.txt will download myfile.txt.
I found the answer here : Download a file from NodeJS Server using Express . Both using express and without using express.
It is too simple if you are using Express. Here is the documentation for res.download. I can't believe that the solution is just one line of code :
res.download('/path/to/file.txt', 'newname.txt');
Here is the code that can make this happen:
app.get('/files/:file', (req, res) => {
res.download(`./uploads/${req.params.file}`, req.params.file.substring(req.params.file.indexOf('_') + 1))
})
I removed the app.use('/files', express.static('uploads')); because the new route handles all the files in the uploads folder.
app.get('/files/:file', (req, res) => { takes the filename from the url as parameter. With this we can just send the file as download with res.download(`./uploads/${req.params.file}.
The filename can be edit to remove the suffix. First we look where the first _ apears, take this index + 1 and create a substring of the string after the suffix: req.params.file.substring(req.params.file.indexOf('_') + 1)

Discord Bot (node.js) : read data from external file

I set up my discord BOT using node.js. For my advantage, I would need to store some data on a external file, but I don't seem to be able to access it from my index.js file (the main Bot file).
I've tried having one static array in the external js/json files, but I can only retrieve undefined/empty values. Additionally, when I tried with a .txt file, once retrieved the content, I found it unable to call functions such as string.split().
Did I miss something in the package content perhaps?
Assuming the data you are storing is in UTF-8 encoding:
var fs = require('fs');
fs.readFile('path/to/file', 'utf8', function(err, contents) {
// code using file data
});
Assuming no errors contents will be a string of the data that is inside that file.
https://code-maven.com/reading-a-file-with-nodejs

How can I respond to an API call with a CSV file? NodeJS

I don't know where to start, any help would be appreciated. Here is what I have so far,
res.setHeader('Content-disposition', 'attachment; filename='+filename);
res.set('Content-Type', 'text/csv');
res.send(filename);
With the above code, it responses with the filename only. I want to respond with the file attached so that I can get the file in front-end (angular4) and pop up with a downloadable link. Any ideas how can I achieve that?
P.S. I am following this tutorial for the front-end. It says that I have to have content-disposition with the file attached in order to make a downloadable link for the file. Not sure if I am doing it right.
https://shekhargulati.com/2017/07/16/implementing-file-save-functionality-with-angular-4/
You need to read the contents of csv and send it response.
var fs = require('fs');
fs.readFile(inputPath, function (err, fileData) {
res.status(200).send(fileData);
})
Hope it helps.

NodeJS write base64 encoded image to file

I have searched and tried all of the solutions at stackoverflow but none of them seems working at my instance. Basically I have an image, edited by html5 canvas, upload from client and I need to save it to disk, but unfortunately I can't open the file that I just saved. (I am using Windows 7)
My code:
var base64Data = req.body.image.replace(/^data:image\/(png|gif|jpeg);base64,/,'');
require('fs').writeFile('public/aboutToGiveUp.png', new Buffer(base64Data, 'base64'));
Got same bug, it's because of incorrect url path, You may added app.use("/", express.static(path.join(__dirname, 'public')));, so no need to add the public in the url, check your url path once.
working sample :
url = req.protocol+'://'+req.headers.host+"/"+filename;
url = req.protocol+'://'+req.headers.host+"/images/"+filename; // its in public/images
Try using ./public/aboutToGiveUp.png or make sure that the path is relative to the file containing this code.

Express res.download() not actually downloading file

I'm attempting to return generated files to the front end through Express' res.download function. I'm using chrome, but whenever I call that API that executes the following code all that is returned is the same values returned from the Express res.sendFile() function.
I know that res.download uses res.sendFile, but I would like the download function to actually save to the file system instead of just returning the file in the body of the response.
This is my code.
exports.download = function(req,res) {
var filePath = //somefile that I want to download
res.download(filePath, 'response.txt', function(err) {
throw err;
}
}
I know that the above code at least partly works because I'm getting back, in the response, the contents of the file. However, I want it to be saved onto the file system.
Am I misunderstanding what the download function is supposed to do? Do I just need to take the response data and write it to the file system manually?
res.download adds headers that suggest to the browser that the file should be downloaded rather than opened. However, there's no way to force the browser to do this; it's ultimately the user's choice whether to download a particular file, typically.
If you're triggering this request with AJAX, well, that's not going to cause it to be downloaded, because your JavaScript is requesting that it get the data.
Do I just need to take the response data and write it to the file system manually?
You don't have file system access in browser-side JavaScript. I'm not sure how you intend to do this.

Resources