31

How do you send files on node.js/express.

I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?

What should the headers be.

What if it's a very large file on a couple of GBs?

Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?

Please give me some information about this.

Thanks!

3 Answers 3

59
app.get('/img/bg.png', function(req, res) {
  res.sendFile('public/img/background.png')
})

https://expressjs.com/en/api.html#res.sendFile

use "res.sendFile". "res.sendfile" is deprecated.

Sign up to request clarification or add additional context in comments.

3 Comments

express deprecated res.sendfile: Use res.sendFile instead
If path needs to be relative, you need to use var path = require('path'); router.get('/GetAudio', function(req, res, next) { res.sendFile(__dirname + "/public/img/background.png"); });
What is the point of this? It's just creating an alias, but you could've just moved the file to that location
6

For large files, you will want to use node.js's concept of piping IO streams together. You want to open the local file for reading, start the HTTP request to rackspace, and then pipe the data events from the file read process to the HTTP request process.

Here's an article on how to do this.

Superagent is fine for small files, but because the superagent API presumes your entire request body is loaded into memory before starting the request, it's not the best approach for large file transfers.

Normally you won't need to worry specifically about the request headers as node's HTTP request library will send the appropriate headers for you. Just make sure you use whatever HTTP method your API requires (probably POST), and it looks like for rackspace you will need to add the X-Auth-Token extra header with your API token as well.

2 Comments

Unfortunately that link is dead. Kind of why Stackoverflow requires you to write out solutions. ;) This is the first post I found close to the issue I'm having but I can't solve it now.
The original question is approaching 10 years old now. I suggest you write a new high-quality question with code snippets of what you are trying to do and what you have tried so far. A significant amount of the entire node.js ecosystem has changed substantially.
5

I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?

You should never use fs.readFileSync in general. When you use it, or any other method called somethingSync, you block the entire server for the duration of that call. The only acceptable time to make synchronous calls in a node.js program is during startup.

What should the headers be.

See RackSpace Cloud Files API.

Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?

While I don't have any experience with superagent, I'm sure it will work fine. Just make sure you read the API documentation and make your requests according to their specification.

1 Comment

I would argue that if you're making some sort of CLI it is is also an acceptable time to use a sync methods as you are running for only a single user and there is no worry of blocking the main thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.