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 express = require('express');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const unzipper = require('unzipper');
const { exec } = require('child_process');
const app = express();
const PASSWORD = 'hello word123';
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' });
}
const filePath = path.join(PUBLIC_FOLDER, `${version}.zip`);
fs.renameSync(file.path, filePath); // 将临时文件移动到目标路径
fs.createReadStream(filePath)
.pipe(unzipper.Extract({ path: PUBLIC_FOLDER }))
.on('close', () => {
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}`);
}
});
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
项目依赖:
{
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.3",
"multer": "^1.4.5-lts.1",
"unzipper": "^0.10.14"
}
}
- 使用pm2启动服务 pm2 start server.js
- postman 使用form-data上传前端页面zip包,约定密码 hello word123。