NodeJS download file from WebServer using POST response - node.js

I am completely lost here regarding this.
I have a custom API where the endpoint is /api and my client NodeJS script calls towards this endpoint with some form data.
Let's say my client sends a POST with the parameters download_file and file_id,
how would the web server respond with the file data?
I'm sure someone has solved this before but I can't find any information about this.

In the client script where you're going to download the file there are multiple steps you need to do:
create a writestream to a file
make a request to the server
pipe the response stream to the file write stream
The code would look something like the following:
const request = require('request');
const fs = require('fs');
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))

Related

how to create a readable stream from a remote URL and pass it into another API without saving it into the local file system in nodejs using got?

I want to access an image from a remote URL and pass it into another API request as request body.
I am using got stream API library to stream the data from the external url.
const url = "https://media0.giphy.com/media/4SS0kfzRqfBf2/giphy.gif";
got.stream(url).pipe(createWriteStream('image.gif'));
const response= await axsios.post('post_url',fs.createReadStream('image.gif');
The download operation is working as expected, but I don't want write the data to the local system.Instead, I woud like pass the response from the got stream API to another API as a request body.

multipart/form-data fetch post gives random output and multer can't extract file

I tried to upload an image file to my server. My client is writing with react native and server is node. I use fetch POST and multipart/form-data on the client side, and multer on server side. When I tried to console.log the request, I got a tons of trash information. I followed the example provided online so I have no idea what's going on. Also multer doesn't extract file from request (req.file is undefined). Also I don't really understand if you post local uri instead of base64 data to your server. How does the server know how to retrieve upload the file through local uri.

How to receive a zip file in koa server

I'm implementing a webserver using Koa. One of my request handlers needs to receive and store a large zip file. The file is NOT uploaded from a web page, but sent from another NodeJs application. Therefore it's not multipart encoded, but just applcation/octet-stream. How can I read such a file from the request?
I've noticed there is a request.socket object but I could not find any documentation on how to use it.
In other words I need the opposite to
this.body = fs.createReadStream(path);

Node.js: How to stream remote file through my server to the user?

There's a large binary file at somwhere.com/noo.bin
I want to send this to the user on my web app.
I don't want to save this file on my server and then serve it, wondering if there's a way to stream the file in which my web app acts as the proxy (the file will look like mysite.com/noo.bin)
Install request, then:
var http = require('http'),
request = require('request'),
remote = 'http://somewhere.com';
http.createServer(function (req, res) {
// http://somewhere.com/noo.bin
var remoteUrl = remote + req.url;
request(remoteUrl).pipe(res);
}).listen(8080);
Though I would have written exactly #LaurentPerrin's answer myself, for completeness sake I should say this:
The drawback in that method is that the request headers you are sending to somewhere.com are unrelated to the request headers your server got. For example: if the request sent to you has a specific value for Accept-Language, it is likely that (as the code stands) you are not going to specify the same value for Accept-Value when proxying from somewhere.com. Thus the resource might be returned to you (and then from you to the original requester) in the wrong language.
Or if the request to you comes in with Accept-Encoding: gzip, the current code will get the large file uncompressed, and will stream it back uncompressed, when you could have saved bandwidth and time by accepting and streaming back a compressed file.
This may or may not be of relevance to you.
If there are important headers you feel you need to pass, you could either add some code to explicitly copy them from your request to the request you are sending somewhere.com, and then copy relevant response headers back, or use node-http-proxy at https://github.com/nodejitsu/node-http-proxy.
An example for a forward proxy using node-http proxy is https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/forward-proxy.js

Node.js Express app with request module - Upload image to CouchDB using PUT

I am using Mikeal's request module to talk to the CouchDB HTTP API from an Express app. I can't use any other modules (such as nano) as this an academic project.
What I would like to do is get an image file, that has been uploaded from a form in a web app, and save it to my Couch as an attachment. To do this using curl is very simple:
curl -vX PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689
--data-binary #artwork.jpg -H "Content-Type: image/jpg"
What I can't wrap my head around is how to structure the PUT request using the request module. Express stores the file temporarily in the directory
/tmp/{doc._id}.jpg
How do I get the file from there to my couch? Pretty desperate here.
I haven't verified this, but you might be able to do this:
var
fs = require('fs'),
request = require('request'),
url = 'http://admin:password#127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689';
var requestStream = request.put(url);
requestStream.on('response', function (response) {
console.log(response);
});
fs.createReadStream('image.jpg').pipe(requestStream);

Resources