简体   繁体   中英

How to deploy expressjs static templates to AWS without restarting the server?

I have express.js server server that I deploy to AWS elastic beanstalk. I use handlebars engine for templating. Templates structure changes more often than server code so I want to have a fast way to deploy them. I have experimented with hosting templates on separate S3 static bucket and then loading them from nodejs code. This way I can deploy changes to templates really fast by using S3 static deployment and not eb deploy that restarts all server nodes.

Here is my node.js server code for template loading:

const s3 = new aws.S3();
s3.getObject({Bucket: 'bucketname.com', Key: 'views/' + path}, (err, data) => {
  const template = handlebars.compile(data.Body.toString('utf-8'));
  resolve(template);
});

This solved template deployment speed problem, but I'm not sure if it does not introduce additional performance hit. I thought since S3 and elastic beanstalk are both amazon services the impact should be minimal, but website benchmarks make me think otherwise.

Maybe there is a better way to solve the problem of templates deployment speed?

Regarding the benchmarking numbers, I believe that's because your code will try to fetch the view template from s3 each time it needs to render the view. These network calls will lead to overhead.

I once faced a similar challenge in one of my previous projects. However, in our case, the size and number of these frequently changing templates were within the acceptable range and it was possible to have them in-memory.

We ended up in creating a different GIT repo for storing these static templates. Our express server would load these templates and cache them in-memory when it is started.

We configured a webhook to ping our express server every time the templates changed (new commit in the git repo). The server then invalidates its cache and loads new templates from the same repo. That way we didn't need to restart the server for every change in the static template.

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