Brian Salehi
Brian Salehi

Reputation: 414

efficient way to send files using express.js

Here's a question I don't even know how to search for it in the web and probably it's a dumb question:

Would it be more efficient for a web server to set download requests and file streams on different ports like 21(ftp) than using default 80/443(http/https) ports? or even use another server to handle these requests so that there would be no longer heavy traffics over http/https connections.Is it even possible to do such action?

If yes, how can I set these settings on Express.js? and if not, what's the regular way of handling heavy downloads?

I know that it's possible to send a file over user's browser using response object like this:

app.get('/download', function(req, res){
    res.download('image.jpg');
}

And as far as I know it uses http/https. I want to know if it's possible to set this on port 21 manually.

So there are two questions:

  1. Is it efficient to handle downloads on different port?
  2. How is it possible to set responses on different port?

thanks for clearing this up

Upvotes: 0

Views: 265

Answers (1)

jfriend00
jfriend00

Reputation: 707456

Putting downloads on a different port does not make a difference to the server efficiency or scale all by itself.

What does help is moving the download of static files out of the node.js process and into another process or, in large scale cases, onto another host. In all cases, I would not add the complication of additional processes until you have proven that you actually have an issue with scale and need to add that extra complexity. Scale bottlenecks are often not where you think they are so adding complexity to a project to improve scale without first measuring where your bottlenecks actually are is usually wasted effort.

Switching to a different port does not increase scalability all by itself. It isn't ports that have scale limits. It's processes and hosts. So, you can add another HTTP process (and stay with HTTP) to successfully add a process and increase scale.

Once you've proven you do actually have a scaling bottleneck with static downloads, here are some things you can do with your static downloads to increase download scale:

  1. Cluster your app so you have multiple node.js processes all contributing to your app scale.
  2. Use a specially built proxy such as NGINX and configure it to handle your static downloads before they get to your node.js process. In that configuration, the NGINX proxy intercepts the static download requests and handles them in its process while passing other requests through to your node.js server.
  3. Use a CDN (a purpose built service for static downloads). For common libraries such as jQuery, there are already publicly available CDNs you can use. Using these will also increase the likelihood that your client has already cached the relevant file (because some other website they used was using the same file on the same CDN).
  4. Deploy your own additional host for static downloads. For example, you will see some large scale web-sites that use xxdomain.com as their main host and img.xxdomain.com as a separate host for image downloads.
  5. Make sure your static files/server have appropriate configuration/settings so they will be cached appropriately by client browsers. You can test this fairly simply by just reloading a page in a client browser and examining the network tab in the Chrome debugger to see if files that should have been cached where cached and vice versa.

In general you want to avoid using ports other than 80 or 442 for web traffic because many network infrastructures (particular tightly controlled corporate environments) may not allow users to use other ports for web traffic.

Upvotes: 2

Related Questions