The project is simply packaged and launched

1. Create web server via node Create
node project and install express Quickly create web server via express
Host the dist folder generated by vue packaging as a static resource to
installnpm install express

const express = require('express')
// 创建web服务器
const app = express()
// 托管静态资源
app.use(express.static('./dist'))
// 启动web服务器
app.listen(3000,()=>{
  console.log('server is running at http://127.0.0.1')
})

2. Open the gzip configuration to
reduce the file size to make the transfer speed faster

// 安装相应包
npm install compression -D 
// 导入包
const compression = require('compression')
// 启用中间件 一定要把这行代码 写在静态资源托管的上面
app.use(compression())

3. Configure http service to make network transmission more secure
Why enable HTTPS service

  1. The data transmitted by the traditional HTTP protocol is plaintext and insecure
  2. Using HTTPS protocol to encrypt the transmitted data, cross-domain to prevent the data from being stolen by middlemen, more secure

Apply for a certificate SSl https://freessl.orgindividual is free

  1. Enter the official website, enter the domain name to be applied and select the brand
  2. Enter your email and choose related options
  3. Verify DNS Add TXT record in the domain name management background
  4. After verification, download the SSL certificate (full_chain.pem public key private.key private key)
  5. Copy the generated public key and private key to the server at the same level as app.js

Import the certificate in the background project

const https = require('https')
const fs = require('fs')
const options = {
  cert:fs.readFileSync('./full_chain.pem')
  key:fs.readFileSync('./private.key')
}
https.createServer(options, app).listen(443)

4. Use pm2 management applications
pm2 role when the node services to close the site can still be normal access to
the global installed npm install pm2 -g
to start the project pm2 start app --name 自定义名称
view the running project pm2 ls
to restart the project pm2 start 自定义名称
to stop the project pm2 stop 自定义名称
delete itemspm2 delete 自定义名称

Published 41 original articles · Likes2 · Visits 1836

Guess you like

Origin blog.csdn.net/weixin_43883485/article/details/105007717