Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 746
samu101108 Avatar asked Jun 08 '20 16:06

samu101108


People also ask

How do I improve node JS application performance?

Caching is one of the common ways of improving the Node Js performance. Caching can be done for both client-side and server-side web applications. However, server-side caching is the most preferred choice for Node Js performance optimization because it has JavaScript, CSS sheets, HTML pages, etc.

Should you minify Nodejs?

Since node. js runs on the server, I'd say it's meaningless to minify the code.


Video Answer


1 Answers

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:

app.use(minify({
  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:

if(process.env.NODE_ENV==='PRODUCTION'){
  app.use(express.static(__dirname + '/static-optimized'));
}else{
  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
like image 175
JRichardsz Avatar answered Oct 19 '22 02:10

JRichardsz