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

不问归期 提交于 2021-01-27 19:51:31

问题


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?


回答1:


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 don perform a manually build process for your production environment like react, angular, etc

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 can use this library uglify-js but you will need a strategy to keep this optimized files in a temp folder like build, dist, etc in client side rendering frameworks (react, vue, etc)

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


来源:https://stackoverflow.com/questions/62267043/which-command-should-i-use-to-minify-and-optimize-nodejs-express-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!