博客
关于我
next项目部署到服务器pm2进程守护
阅读量:789 次
发布时间:2023-02-15

本文共 2037 字,大约阅读时间需要 6 分钟。

一、npm run export导出文件上传到CDN

在项目中执行npm run export后,会生成outCDN文件夹。接下来需要将该文件夹中的文件上传至CDN服务器。

以下是上传文件的具体实现代码:

```javascriptconst fs = require('fs');const path = require('path');const OSS = require('ali-oss');const filePath = path.join(__dirname, '../outCDN');const excludeFiles = ['index.html'];

const client = new OSS({region: 'oss-cn-shenzhen',accessKeyId: '',accessKeySecret: '',bucket: ''});

async function uploadFile(filePath) {fs.readdir(filePath, async function(err, files) {if (err) {console.warn('读取文件失败:%s', err);} else {files.forEach(async function(filename) {const filedir = path.join(filePath, filename);fs.stat(filedir, async function(err, stats) {if (err) {console.warn('获取文件stats失败:%s', err);} else {const isFile = stats.isFile();const isDir = stats.isDirectory();if (!excludeFiles.includes(filename) && isFile) {const fileKey = ${filedir.split('outCDN/').pop()};try {const data = await client.put(fileKey, filedir);console.log('上传成功:%j', data);} catch (err) {console.error('上传失败:%j', err);}}if (isDir) {uploadFile(filedir);}}});});}});}

uploadFile(filePath);

二、处理next build后的文件

Next.js项目中,执行next build后,会自动生成.next文件夹及其子文件。我们需要将这些文件进行归档和管理,以便后续部署。

归档文件的具体实现代码如下:

```javascriptconst fs = require('fs');const path = require('path');const includeFiles = ['package.json', 'server.js', 'next.config.js', 'ecosystem.json'];async function copyFiles(srcDir) { return new Promise((resolve, reject) => { fs.readdir(srcDir, async function(err, files) { if (err) { reject(err); return; } files.forEach(function(filename) { const currentPath = path.join(srcDir, filename); const stat = fs.statSync(currentPath); if (includeFiles.includes(filename)) { const targetPath = path.join('copiedFiles', filename); fs.createReadStream(currentPath).pipe(fs.writeSync(targetPath)); } else if (stat.isDirectory()) { copyFiles(currentPath); } }); resolve({ success: true }); }); });}copyFiles('src');

上述代码实现了文件的递归复制功能,适用于管理Next.js项目的构建文件。

转载地址:http://lxcfk.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0058---Netty群聊系统客户端
查看>>
Netty工作笔记0059---Netty私聊实现思路
查看>>
Netty工作笔记0060---Netty心跳机制实例
查看>>
Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
查看>>
Netty工作笔记0061---Netty心跳处理器编写
查看>>
Netty工作笔记0062---WebSocket长连接开发
查看>>
Netty工作笔记0063---WebSocket长连接开发2
查看>>
vue样式穿透 ::v-deep的具体使用
查看>>
Netty工作笔记0065---WebSocket长连接开发4
查看>>
Netty工作笔记0066---Netty核心模块内容梳理
查看>>
Vue基本使用---vue工作笔记0002
查看>>
Netty工作笔记0068---Protobuf机制简述
查看>>
Netty工作笔记0069---Protobuf使用案例
查看>>
Netty工作笔记0070---Protobuf使用案例Codec使用
查看>>
Netty工作笔记0071---Protobuf传输多种类型
查看>>
Netty工作笔记0072---Protobuf内容小结
查看>>
Netty工作笔记0073---Neety的出站和入站机制
查看>>
Netty工作笔记0074---handler链调用机制实例1
查看>>
Netty工作笔记0075---handler链调用机制实例1
查看>>
Netty工作笔记0076---handler链调用机制实例3
查看>>