react如何将前端包部署到服务器?
1.使用FinalShell工具,首先连接服务器,然后将前端的打包文件放到服务器:
这样每次都要在前端打包,然后再将文件夹放到服务器,为了便捷,采用ftp自动化部署的方式
2. ftp自动化部署
- ftp:
https://www.npmjs.com/package/ftp-deploy
- 首先安装ftp-deploy:
npm i -D ftp-deploy
- 然后新建文件,如 主目录/scripts/ftp.js
var Deploy = require("ftp-deploy");
var ftpDeploy = new Deploy();
var config = {
host: "xxxxx", //服务器地址
user: "xxx", //服务器登录账号
password: "xxxx", //服务器密码
port: 22, //SFTP的默认端口是22
localRoot: "./build",
remoteRoot: "/usr/local/nginx/build/", //远程路径
include: ["*"], //包含文件
deleteRemote: false, //上传前是否删除
sftp:true, //ssh连接,sftp的值为true
}
ftpDeploy.deploy(config, function (err, res) {
if (err) console.log(err);
else console.log("finished:", res);
});
ftpDeploy.on("uploading", function (data) {
// eslint-disable-next-line no-unused-expressions
data.totalFilesCount;
// eslint-disable-next-line no-unused-expressions
data.transferredFileCount;
// eslint-disable-next-line no-unused-expressions
data.filename;
});
ftpDeploy.on("uploaded", function (data) {
console.log(data);
});
ftpDeploy.on("log", function (data) {
console.log(data);
});
ftpDeploy.on("upload-error", function (data) {
console.log(data.err);
});
- 然后修改package.json文件
"scripts": {
"start": "node --max-old-space-size=4096 scripts/start.js",
"build": "node --max-old-space-size=8192 scripts/build.js",
"deploy": "node scripts/ftp.js",
"buildftp": "npm run build && npm run deploy",
"test": "node scripts/test.js"
},
- 使用
npm run buildftp
就可以实现打包+自动化部署