Skip to content

vercel部署优化,修复首页报错找不到readme.md的问题 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
vercel部署优化,修复首页报错找不到readme.md的问题
readme.md文件修改后,需要执行一次node utils/md2html.js来更新index.html的内容
  • Loading branch information
yutons committed Dec 17, 2024
commit 43217eea4fd4650fdd922b09f567c65b85606c62
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dist
.pnp.*
/local/
!README.md

.idea
.vercel
55 changes: 34 additions & 21 deletions controllers/root.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
import path from 'path';
import {readdirSync, readFileSync, existsSync} from 'fs';
import '../utils/marked.min.js';
const LOG_WITH_FILE = process.env.LOG_WITH_FILE;

export default (fastify, options, done) => {
// 添加 / 接口
fastify.get('/', async (request, reply) => {
let readmePath = null;
const files = readdirSync(options.rootDir);
// console.log(files);
for (const file of files) {
if (/^readme\.md$/i.test(file)) {
readmePath = path.join(options.rootDir, file);
break;
if (LOG_WITH_FILE){
let readmePath = null;
const files = readdirSync(options.rootDir);
// console.log(files);
for (const file of files) {
if (/^readme\.md$/i.test(file)) {
readmePath = path.join(options.rootDir, file);
break;
}
}
}

// 如果未找到 README.md 文件
if (!readmePath) {
let fileHtml = files.map(file => `<li>${file}</li>`).join('');
reply.code(404).type('text/html;charset=utf-8').send(`<h1>README.md not found</h1><ul>${fileHtml}</ul>`);
return;
}
// 如果未找到 README.md 文件
if (!readmePath) {
let fileHtml = files.map(file => `<li>${file}</li>`).join('');
reply.code(404).type('text/html;charset=utf-8').send(`<h1>README.md not found</h1><ul>${fileHtml}</ul>`);
return;
}

// 读取 README.md 文件内容
const markdownContent = readFileSync(readmePath, 'utf-8');
// 读取 README.md 文件内容
const markdownContent = readFileSync(readmePath, 'utf-8');

// 将 Markdown 转换为 HTML
const htmlContent = marked.parse(markdownContent);
// 将 Markdown 转换为 HTML
const htmlContent = marked.parse(markdownContent);

// 返回 HTML 内容
reply.type('text/html').send(`
// 返回 HTML 内容
reply.type('text/html').send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>README</title>
<link href='/public/css/main.css' rel='stylesheet' type='text/css'/>
<title>drpyS(drpy-node)</title>
</head>
<body>
${htmlContent}
</body>
</html>
`);
}else{
// 设置文件路径
const indexPath = path.join(options.rootDir, 'public', 'index.html');

// 读取 index.md 文件内容
const indexContent = readFileSync(indexPath, 'utf-8');

// 返回 HTML 内容
reply.type('text/html').send(indexContent);
}
});

// 新增 /favicon.ico 路由
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"fastify": "^4.15.0",
"fastq": "^1.17.1",
"lodash": "^4.17.21",
"marked": "^15.0.4",
"mime-types": "^2.1.35",
"p-queue": "^8.0.1",
"qs": "^6.13.1",
Expand Down
Loading