[Fixed] Which command should I use to minify and optimize nodejs express application?


I am ready with an Express-generator sccafold website and need to publish it.
Which command should I use to minify files and be optimized for publishing?
And also, what are the directories should I take to upload?


express-generator is a server rendering framework based on express framework, not a client side rendering like react, vue, angular, etc in which a minify process is very common.

This question: Does it make sense to minify code used in NodeJS? indicates me that nodejs is already performing optimizations for nodejs code.

So, if we are are talking about express app, just a static files are candidates to minify to improve performance.

minify on the fly

In this case, you could perform a manually build process for your production environment like client side frameworks(react, angular, vue, etc) does

Check this library: express-minify at which we can minify several types of files:

  cache: false,
  uglifyJsModule: null,
  errorHandler: null,
  jsMatch: /javascript/,
  cssMatch: /css/,
  jsonMatch: /json/,
  sassMatch: /scss/,
  lessMatch: /less/,
  stylusMatch: /stylus/,
  coffeeScriptMatch: /coffeescript/,

manually minify

In this case you could use this library uglify-js but you will need a strategy to keep this optimized files in a temp folder like build, dist, etc like client side rendering frameworks (react, vue, etc) does

You could do something like this:

  app.use(express.static(__dirname + '/static-optimized'));
  app.use(express.static(__dirname + '/static'));

And finally execute the minify process over each file:

uglifyjs ./static/my-code.js --output ./static-optimized/my-code.min.js

To avoid manual process, you can write a routine to iterate all js files and execute uglifyjs one by one. This could be your build script in your package.json

6 Easy Ways to Speed Up Express

From: https://stackabuse.com/6-easy-ways-to-speed-up-express/

  • gzip Compression
  • Run Express in Production Mode
  • Minify with Uglify
  • Reduce Your Middleware
  • Increase Max Sockets
  • Use Cache-Control

Leave a Reply

(*) Required, Your email will not be published