![]() ![]() This where the real magic happens when it comes to ability to serve massive spikes in traffic. Any future requests to the same path received within the expiry period will be served directly from the CDN without going to the server. When you serve your Express app through a CDN, once the first request passes through, the content delivery network will capture it and put it into cache. That way, you can have a precise control of the caching behaviour right from your app and down to the single route level. You want your CDN to honour the cache-control header values that your app is sending rather than overwrite with a generic setting. The key parameter to set up in your CDN configuration is the caching behaviour. In many cases it is easier to integrate if you go with the stack from your main cloud provider but other than convenience, the basic principles behind major CDNs are the same. There are some good articles on the server side cache but with the implementation of cache-control headers, you do not need all that hassle! For most use cases, using a CDN to leverage cache-control is much leaner, cheaper, faster and more resilient than building your own server side cache mechanism.įor example, if you are already running your Express app on AWS, you can consider using their CDN CloudFront - which has points of presence in over 70 cities across over 35 countries in the world.Īll other major cloud providers offer their own CDNs and if you are looking for an independent option, you could consider CloudFlare. The next step is to set up the content delivery network. Your Express app will now send cache-control headers configured to your specifications. now call the new middleware function in your app remember to call next() to pass on the request next() for the other requests set strict no caching parameters The simplest middleware to set cache-control would be to set the cache header for the entire application as follows: app. Setting cache control middleware in Express We will create a caching middleware to help automatically set the right header for the entire Express app. ![]() PRIVATE CACHE TYPE CODEIt would be very cumbersome to apply the code above for every single route. set( 'Cache-control', 'public, max-age=300') ![]() You can set HTTP headers in an Express app using the response api: res. Here is how the header looks like to enable public 5 minut cache: cache- control: public, max-age= 300 Setting Cache-control header in Express So for example, if you publish a lot of new content, your home page could be cached for only 5 minutes but pages with articles or blog posts could be cached for a day. Anything from 5 minutes to a day (or even a few days) is probably a good choice for most 'publishers' depending on the frequency they publish and refresh content. You want to set this as long as possible without compromising the ability to refresh your content where it is changing. Max-age - in seconds specifies how long the content is supposed to be cached. This is the setting you want for most of your content representing articles, blogs, 'static' pages, product pages etc. That means that every layer that your response passes through is allowed to cache your content and serve it. Public - indicates that the response may be cached by any cache. The following are the key parameters that we will use in our example configuration: PRIVATE CACHE TYPE FULLYou can find the full specification of Cache-control at MDN. Nginx), your CDN and client browsers will cache content and serve it instead of forwarding requests to the app. The http header Cache-control allows to define how your proxy server (e.g. We will not be using any server side caching, as the assumption is that with the right configuration of the HTTP headers and the CDN there will be no need to burden your server with any additional requests related to server-side caching. We will leverage the power of HTTP cache-control headers combined with caching and serving your Express app via CDN network. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |