Start gzip in node environment of project optimization

A. Create server through node
Use the terminal to open the vue_demo_server folder. Enter the command NPM init-y

  • to initialize the package. Then enter the command NPM I express-s
  • to open the vue_demo directory and copy the dist folder generated after executing the NPM run build command. Paste into the vue_demo_server
  • in the vue_demo_server folder to create the app.js file, and write the following code:
  • const express = require('express')
    
    const app = express()
    
    app.use(express.static('./dist'))
    
    app.listen(8998,()=>{
        console.log("server running at http://127.0.0.1:8998")
    })
    
    • and then enter node app.js in the terminal again to start the project.

    B. Open gzip compression

    • open the terminal of the vue_demo_server folder, enter the command: NPM I compression -d
    • open app.js, and write the code:
    const express = require('express')
    
    const compression = require('compression')
    
    const app = express()	
    
    app.use(compression()) // 注册中间件 一点要写在静态托管之前
    app.use(express.static('./dist')) //
    
    app.listen(8998,()=>{
        console.log("server running at http://127.0.0.1:8998")
    })
    

    Js
    open the page and you will find:

    relatively large files have been gzip enabled.

    Read More: