-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (48 loc) · 1.51 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require('express');
const path = require('path');
const app = express();
const fs = require('fs');
// 设置静态文件目录
const staticDir = path.join(__dirname, 'dist');
// 自定义中间件来处理没有后缀的请求
app.use((req, res, next) => {
const requestedPath = path.join(staticDir, req.path);
console.log('requestedPath', requestedPath);
// 如果是index.html
if (req.path === '/') {
req.url = '/index.html';
}
// 如果请求的路径没有后缀
if (path.extname(req.path) === '') {
// 检查是否是目录
if (fs.existsSync(requestedPath) && fs.lstatSync(requestedPath).isDirectory()) {
// 如果是目录,检查是否存在 index.js
const indexPath = path.join(requestedPath, 'index.js');
if (fs.existsSync(indexPath)) {
req.url = path.join(req.path, 'index.js');
} else {
// 处理没有 index.js 的情况
res.status(404).send('Index file not found in directory');
return;
}
} else {
// 如果不是目录,尝试补充 .js 后缀
const jsPath = `${requestedPath}.js`;
if (fs.existsSync(jsPath)) {
req.url += '.js';
} else {
// 处理文件不存在的情况
res.status(404).send('File not found');
return;
}
}
}
next();
});
// 设置静态文件目录
app.use(express.static(staticDir));
// 启动服务器
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});