我应该使用哪个命令来最小化和优化nodejs express应用程序? [英] Which command should I use to minify and optimize nodejs express application?

查看:74
本文介绍了我应该使用哪个命令来最小化和优化nodejs express应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经准备好Express-generator sccafold网站,并且需要发布它.我应该使用哪个命令来缩小文件并针对发布进行优化?而且,我应该上传什么目录?

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是基于Express框架的服务器渲染框架,而不是像React,Vue,Angular等的客户端渲染,其中最小化过程非常简单.常见的.

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.

此问题:>在NodeJS中使用的代码是什么?表示我nodejs已经在对nodejs代码进行优化.

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

因此,如果我们谈论的是Express App,那么只需静态文件即可缩小性能,以提高性能.

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

在这种情况下,您可以针对生产环境执行手动构建过程,例如客户端框架(反应,角度,vue等)所做的

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

检查此库: express-minify ,我们可以在其中缩小几种类型的文件:

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/,
}));

手动缩小

在这种情况下,您可以使用此库 uglify-js ,但您需要一种将优化后的文件保存在诸如build,dist等临时文件夹中的策略,例如客户端渲染框架(react,vue等)

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

您可以执行以下操作:

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

为避免手动处理,您可以编写例程来迭代所有js文件并逐一执行uglifyjs.这可能是您package.json

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

来自: https://stackabuse.com/6-轻松加快表达速度/

  • gzip压缩
  • 以生产模式运行Express
  • 用Uglify缩小
  • 减少中间件
  • 增加最大套接字数
  • 使用缓存控制

这篇关于我应该使用哪个命令来最小化和优化nodejs express应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆