Express downloadable response from stream - javascript

I have an end point which should respond back with an excel file. Right Now I'm able to write the file to a write stream,pipe it to the disk and respond with the downloadable file.
var fs = require('fs');
const writeStream = fs.createWriteStream('abc.xlsx');
//stream is the stream object
stream.pipe(writeStream);
res.download('abc.xlsx')
This works fine and the browser downloads abc.xlsx file. Is there anyway I could do this without storing the file to the disk just by using the stream object?

Related

Downloading File From Client Side Node.js

so I am trying to build a website that allows users to download files that are located in the server computer when the users access the website and click a download button.
I wish to use as few libraries as possible due to some real world limitations. Ideally no Express or Ajax. And I think it should be fully possible with just vanilla node.js
From my search on the internet it seems most of the code is of this form:
const fs = require('fs');
const https = require('https');
// URL of the image
const url = 'GFG.jpeg';
https.get(url,(res) => {
// Image will be stored at this path
const path = `${__dirname}/files/img.jpeg`;
const filePath = fs.createWriteStream(path);
res.pipe(filePath);
filePath.on('finish',() => {
filePath.close();
console.log('Download Completed');
})
})
However, the code doesn't seem to be doing what I want.
First, it requires an url, so it is more about directing a resource online to another location. Whereas I want to actually serve a locally stored file on the server to users when they access the website.
Second, it appears to be downloading to the server computer. But what I want is to let users download to their own client devices. Basically the normal download function you would encounter when you want to download something on the Internet and you see your browser's "Download" section having some new entries.
How can I achieve what I want?
I'm a total noob at this, so it would be great if I can get a skeleton code with some dummy file or pathname.
Appreciate any guidance. Thanks!
You are missing an http.server. http.get just does a web request and as you said you don't want to do that.
Here is some example code creating a server and serving a single file without using express:
const fs = require('fs');
const path = require('path');
const http = require('http');
http.createServer(function(request, response) {
var filePath = path.join(__dirname, '/files/img.jpeg');
response.writeHead(200);
var readStream = fs.createReadStream(filePath);
readStream.pipe(response);
}).listen(2000);

upload pdf file from storage to firebase in react-native

I have been trying to upload PDF file from phone storage to Firebase. Its working fine when I use react-native-document-picker to select a pdf file, which returns the path of the file something like content://com.android.externalstorage.documents/document/primary%3Adocs%2F' +'<filename>'.pdf'
which is then passed to fetch
const response = await fetch(path);
blob = await response.blob();
and then the blob is uploaded to Firebase.
But the problem is that I don't want to pick a document every time. if I don't use react-native-document-picker and just curate the exact path return by the module. it gives network error when passed to fetch, so unable to create a blob.
I also tried react-native-fs but couldn't figure out how to create a blob of PDF residing in emulated storage.

NW.js + PDF.js: How to provide an URL to pdf.js?

I'm quite new to Javascript.
I want to make a very simple native PDF viewer using nw.js and pdf.js. It reads a package file and extract a buffer that contains a valid PDF file. Then I convert the buffer into a stream like this:
var stream = require('stream');
var bufferStream = new stream.PassThrough();
bufferStream.end(pdfbuffer); // pdfbuffer is the PDF file's buffer
But I don't know how to make a valid URL for this PDF file so that I can fill it in pdf.js' default viewer code here:
// viewer.js from PDF.JS
var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf';
Or am I thinking of the wrong direction?
I think you should create a Writable stream first. Then write that stream to a PDF file which you will create too. I'm not sure if you can use a PDF file as a writable stream directly. Sorry if this doesn't really answer the question but take this as a nudge in the right direction.

How to save an audio stream as a wav file using NodeJS

My aim is to record a radio stream coming from an Icecast server.
I am using the icecast node module to fetch the radio stream and then writing the wave file by piping the stream through the wav module.
Here is an example of my code:
const icecast = require('icecast');
const url = 'http://87.118.104.139/radiogibsonaac';
var wav = require('wav');
let ice, fileWriter;
ice = icecast.get(url, res => {
fileWriter = new wav.FileWriter(__dirname+'/recording.wav', {
channels: 1,
sampleRate: 16000,
bitDepth: 128
});
res.pipe(fileWriter);
});
setTimeout(()=>{
fileWriter.end();
ice.end();
},5000);
The stream is successully recorded to my disk as expected and I am able to listen to the file in VLC, but the wav file itself does not seem to be formed correctly.
When I try to use another tool to edit the file, it shows an error each time.
For example, I am trying to change the speed of the audio on this site and it does not recognise the file.
Also if I try to view the file info using the Sox CLI it displays:
sox FAIL formats: can't open input file `recording.wav': Sorry, don't understand .wav size
Does anybody know if I am missing a step in the process of writing the wav file to disk?
Based on the stream URL, it looks like the stream is in AAC format, and you are trying to write that data directly to a WAV file, so you end up with a file with a WAV header but AAC audio data.
You would need to either write the stream to disk as AAC, and then do a conversion on the file, or transcode the stream on-the-fly before writing it to disk.

create and save excel file with nodejs

I am trying to create and save an excel file. I used this module
and followed their example. In my case I want to save the result locally. Somehow like this:
var fs = require('fs');
fs.writeFile(filename, result,"binary");
I cannot open the file via excel, because it's saying that the file is corrupted or broken. Looking again at the example of the module, I know that result is in a binary format.
What can I do to parse result correctly for saving the excel file?
Thanks in Advance.

Categories