简体   繁体   中英

How to serve static content for a Node.js application?

Quick question about the correct server infrastructure for a Node application I'm building. The application has a web frontend built with Angular that uses the API I built in Node. This API will also be eventually accessed by a mobile application. In terms of server infrastructure, what is the fastest/cleanest way to do set this up? I would ordinarily do a simple 3 server setup, with a node server, a mongodb server, and an nginx server to act as a proxy, and have the node server serve all static files for the web frontend, but I'm not sure if this is the best practice here. So my two questions are:

  1. Would it be better to have the nginx server serve all static content for the web frontend and only have Node serve the API?

  2. The application allows users to upload files to be stored on the server. Where is the best place to store and serve these files?

Here's a basic Nginx config to service static content and pass everything else to a Node.js app:

    location / {
      try_files $uri $uri/ @backend;
    }

    location @backend {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://127.0.0.1:3000;
      proxy_redirect off;
    }

I recommending having Nginx serve the static content because Nginx does an excellent job at that frees up Node.js to focus on serving the app with fewer requests getting to it.

File uploading is handled in your Node.js. The only part handled in Nginx is tuning the file upload size limit. You can limit this to particular location blocks as needed:

client_max_body_size 20m;

There are several file-uploading packages for Node.js. You can choose between solutions that stream the uploads with an events interface, like connect-busboy or a more traditional approach where all the request is completely finished before you get access to it.

Where you store your uploads is up to you. It's increasingly popular to pass file uploads on to a storage service like Amazon's S3 instead of storing them locally. S3 provides cheap storage, fast bandwidth and redundant storage, but a local directory may meet your needs as well.

Depending on the requirements for your app, the setup I use is:

  • Heroku for the node.js web service (with either mongodb or postgresql for the db)
  • Amazon's s3 for my static assets, if they need to be private you can set up aa cloudfront distribution for serving private assets from your s3 bucket.

If you're looking for fast and clean I'd really recommend using heroku, but again, depends on your specific requirements.

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM