使用express+nginx+pm2+postman实现推送zip包自动更新前端网页

1.nginx配置将80端口代理到项目的3000端口

server {
    listen       80; #监听的端口
    server_name  localhost; #监听的域名
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
		#root html;
		#index index.html index.html;
        proxy_pass http://127.0.0.1:3000; #转发请求的地址
		proxy_connect_timeout 600;
		proxy_read_timeout 600;
}

Windows下的常用命令

启动服务:start nginx
退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload  (重载服务配置文件,类似于重启,服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
使用帮助:nginx -h

2.express启动网页服务,还有一个post上传接口包括文件(file),版本号(version),约定密码(password),其中目录结构中页面在服务端项目根目录下public文件夹下。server.js也在项目的根目录下。前端启动是3000端口。
server.js

const fs = require('fs');
const path = require('path');
const multer = require('multer');
const express = require('express');
const AdmZip = require('adm-zip');

const app = express();
const PASSWORD = 'hello world';
const PUBLIC_FOLDER = path.join(__dirname, 'public');
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// 配置 multer 中间件
const upload = multer({
    dest: 'temp/', // 设置临时存储目录
});

app.post('/upload', upload.single('file'), (req, res) => {
    const version = req.body.version;
    const password = req.body.password;
    const file = req.file;

    if (password !== PASSWORD) {
        fs.unlinkSync(file.path); // 删除临时上传的文件
        return res.status(401).json({ error: 'Invalid password' });
    }

    try {
        if (!fs.existsSync(PUBLIC_FOLDER)) {
            fs.mkdirSync(PUBLIC_FOLDER);
        }
        const filePath = path.join(PUBLIC_FOLDER, `${version}.zip`);
        fs.renameSync(file.path, filePath); // 将临时文件移动到目标路径
        const zip = new AdmZip(filePath);
        zip.extractAllTo(PUBLIC_FOLDER, true);
        fs.unlinkSync(filePath); // 删除上传的压缩文件
        res.json({ message: version + ' File uploaded and extracted successfully' });

        // exec('pm2 restart 0', (error, stdout, stderr) => {
        //     if (error) {
        //         console.error(`Failed to restart server: ${error.message}`);
        //     } else {
        //         console.log(`Server restarted: ${stdout}`);
        //     }
        // });
    } catch (error) {
        fs.unlinkSync(filePath); // 删除上传的压缩文件
        res.status(500).json({ error: 'Error extracting the ZIP file' });
    }
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});


项目依赖:

{
  "dependencies": {
  	"adm-zip": "^0.5.14",
    "body-parser": "^1.20.2",
    "express": "^4.18.3",
    "multer": "^1.4.5-lts.1"
  }
}
  1. 使用pm2启动服务 pm2 start server.js
  2. postman 使用form-data上传前端页面zip包,约定密码 hello world

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值