fix: 获取运行时根路径不一致的bug
This commit is contained in:
+10
-4
@@ -28,9 +28,12 @@ export default CreateServer
|
||||
* 创建服务
|
||||
* port: 服务端口号
|
||||
*/
|
||||
function CreateServer(port = 4873) {
|
||||
async function CreateServer(port = 4873) {
|
||||
const __dirname = dirname(fileURLToPath(
|
||||
import.meta.url));
|
||||
const rootDir = process.cwd();
|
||||
|
||||
// console.log(__dirname, rootDir);
|
||||
|
||||
const router = new getRouter();
|
||||
const app = new Koa();
|
||||
@@ -38,7 +41,7 @@ function CreateServer(port = 4873) {
|
||||
const staticPath = './static'
|
||||
app.use(
|
||||
koaMount('/resource/static', koaStatic(
|
||||
path.join(__dirname, staticPath), {}
|
||||
path.join(rootDir, staticPath), {}
|
||||
))
|
||||
)
|
||||
|
||||
@@ -49,7 +52,7 @@ function CreateServer(port = 4873) {
|
||||
|
||||
|
||||
// 初始化路由
|
||||
const initRouter = (folderPath: string) => {
|
||||
const initRouter = async (folderPath: string) => {
|
||||
// 读取文件夹中的文件列表
|
||||
fs.readdir(folderPath, async (err, files) => {
|
||||
if (err) {
|
||||
@@ -72,6 +75,8 @@ function CreateServer(port = 4873) {
|
||||
const filePath = path.join(folderPath, file);
|
||||
// 动态导入文件
|
||||
const module = await import("./" + path.relative(__dirname, filePath));
|
||||
// console.log(path.relative(__dirname, filePath),__dirname,filePath);
|
||||
// const module = await import("./" +filePath);
|
||||
// 在这里可以对导入的模块进行处理,例如调用其中的函数或使用其导出的对象
|
||||
module.default(router);
|
||||
// console.log(module);
|
||||
@@ -83,7 +88,7 @@ function CreateServer(port = 4873) {
|
||||
});
|
||||
}
|
||||
|
||||
(async () => {
|
||||
await (async () => {
|
||||
// 目标文件夹的路径
|
||||
const folderPath = './routers';
|
||||
|
||||
@@ -104,6 +109,7 @@ function CreateServer(port = 4873) {
|
||||
}
|
||||
|
||||
startServer();
|
||||
return app;
|
||||
}
|
||||
|
||||
// const bH = new BackupHelper();
|
||||
|
||||
Reference in New Issue
Block a user