fix
This commit is contained in:
1 parent
233db48aae
commit
f22dd54490
1 file changed
+46
-36
@@ -1,64 +1,74 @@
|
|||||||
import express from 'express'
|
import express from "express";
|
||||||
import os from 'os'
|
import os from "os";
|
||||||
import path from 'path'
|
import path from "path";
|
||||||
import cors from 'cors'
|
import cors from "cors";
|
||||||
import fs from 'fs'
|
import fs from "fs";
|
||||||
|
|
||||||
if (!fs.existsSync('./static')) {
|
if (!fs.existsSync("./static")) {
|
||||||
fs.mkdirSync('./static')
|
fs.mkdirSync("./static");
|
||||||
}
|
}
|
||||||
|
|
||||||
const staticPath = path.join('./static')
|
const staticPath = path.join("./static");
|
||||||
const port = 17705
|
const port = 17705;
|
||||||
|
|
||||||
const prev = `/${'get'}`
|
const prev = `/${"get"}`;
|
||||||
const app = express()
|
const app = express();
|
||||||
app.use(cors())
|
app.use(cors());
|
||||||
|
|
||||||
// 不存在文件与文件夹同名的问题
|
// 不存在文件与文件夹同名的问题
|
||||||
app.use(prev, express.static(staticPath))
|
app.use(prev, (req, res, next) => {
|
||||||
app.use(prev, (req, res) => {
|
const decodedPath = decodeURIComponent(req.path);
|
||||||
const directoryPath = path.join(staticPath, req.path)
|
const directoryPath = path.join(staticPath, decodedPath);
|
||||||
|
fs.stat(directoryPath, (err, stats) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err, directoryPath);
|
||||||
|
// return res.status(500).json({ error: "Unable to access path" });
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.isDirectory()) {
|
||||||
fs.readdir(directoryPath, (err, files) => {
|
fs.readdir(directoryPath, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err, directoryPath)
|
console.log(err, directoryPath);
|
||||||
return res.status(500).json({ error: 'Unable to scan directory' })
|
return res.status(500).json({ error: "Unable to scan directory" });
|
||||||
}
|
}
|
||||||
const fileDetails = files.map((file) => {
|
const fileDetails = files.map(file => {
|
||||||
const filePath = path.join(directoryPath, file)
|
const filePath = path.join(directoryPath, file);
|
||||||
const stats = fs.statSync(filePath)
|
const stats = fs.statSync(filePath);
|
||||||
return {
|
return {
|
||||||
name: file,
|
name: file,
|
||||||
size: stats.isDirectory() ? undefined : stats.size,
|
size: stats.isDirectory() ? undefined : stats.size,
|
||||||
type: stats.isDirectory() ? 'directory' : 'file',
|
type: stats.isDirectory() ? "directory" : "file",
|
||||||
stats: {
|
stats: {
|
||||||
atimeMs: stats.atimeMs,
|
atimeMs: stats.atimeMs,
|
||||||
birthtimeMs: stats.birthtimeMs,
|
birthtimeMs: stats.birthtimeMs,
|
||||||
ctimeMs: stats.ctimeMs,
|
ctimeMs: stats.ctimeMs,
|
||||||
mtimeMs: stats.mtimeMs
|
mtimeMs: stats.mtimeMs
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
res.json(fileDetails);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
res.json(fileDetails)
|
});
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
app.listen(port, '::', () => {
|
app.use(prev, express.static(staticPath));
|
||||||
const interfaces = os.networkInterfaces()
|
|
||||||
console.log(`服务器正在以下地址运行:`)
|
app.listen(port, "::", () => {
|
||||||
|
const interfaces = os.networkInterfaces();
|
||||||
|
console.log(`服务器正在以下地址运行:`);
|
||||||
for (const name of Object.keys(interfaces)) {
|
for (const name of Object.keys(interfaces)) {
|
||||||
for (const iface of interfaces[name]) {
|
for (const iface of interfaces[name]) {
|
||||||
iface.family === 'IPv6'
|
iface.family === "IPv6" ? console.log(`http://[${iface.address}]:${port}`) : console.log(`http://${iface.address}:${port}`);
|
||||||
? console.log(`http://[${iface.address}]:${port}`)
|
|
||||||
: console.log(`http://${iface.address}:${port}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(`以下地址访问文件/文件夹:`)
|
console.log(`以下地址访问文件/文件夹:`);
|
||||||
for (const name of Object.keys(interfaces)) {
|
for (const name of Object.keys(interfaces)) {
|
||||||
for (const iface of interfaces[name]) {
|
for (const iface of interfaces[name]) {
|
||||||
iface.family === 'IPv6'
|
iface.family === "IPv6" ? console.log(`http://[${iface.address}]:${port}${prev}`) : console.log(`http://${iface.address}:${port}${prev}`);
|
||||||
? console.log(`http://[${iface.address}]:${port}${prev}`)
|
|
||||||
: console.log(`http://${iface.address}:${port}${prev}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
Reference in new issue
Block a user