Node.js 要求(path.resolve(filePath))导入语句

Node.js 要求(path.resolve(filePath))导入语句,node.js,typescript,express,import,dynamic-import,Node.js,Typescript,Express,Import,Dynamic Import,我已经用JS编写了ExpressJS应用程序的自动加载脚本,目前我们正在迁移到TypeScript export const loadModels = () => { glob('modules/*/**.model.js', (err, files) => { if (!err) { files.forEach((filePath) => { require(path.resolve(filePath)); });

我已经用JS编写了ExpressJS应用程序的自动加载脚本,目前我们正在迁移到TypeScript

export const loadModels = () => {
  glob('modules/*/**.model.js', (err, files) => {
    if (!err) {
      files.forEach((filePath) => {
        require(path.resolve(filePath));
      });
    }
  });
};
export const loadRoutes = (app: express.Application) => {
  glob('modules/*/**.routes.js', (err, files) => {
    if (!err) {
      files.forEach((filePath) => {
        require(path.resolve(filePath))(app);
      });
    }
  });
};

如何更改
require(path.resolve(filePath))
需要(path.resolve(filePath))(app)
导入
语句


这是示例路由文件

import config from './../../config';
import express from 'express';
import QuestionCtrl from './question.controller';
import { EventEmitter } from 'events';

export default (app: express.Application, events: EventEmitter) => {
  const questionCtrl = new QuestionCtrl(events);
  app.route(`${config.app.apiBase}/questions`).post(questionCtrl.create);
};


你也许可以利用。例如:

export const loadRoutes = async (app: express.Application) => {
  glob('modules/*/**.routes.js', async (err, files) => {
    if (!err) {
      for (filePath of files) {
        const route = await import(path.resolve(filePath));
        route(app);
      };
    }
  });
};

找不到名称“filePath”。对于(文件的文件路径){~~~~~~~~~~~~错误TS1308:“等待”表达式仅在异步函数中允许。常量路由=等待导入(路径.解析(文件路径));~~~~~错误TS2304:找不到名称“文件路径”。常量路由=等待导入(路径.解析(文件路径));现在得到
TypeError:route不是一个函数
route文件的示例是什么?根据在route文件中导出内容的方式,它可能需要类似于
route.exportName(app)
。我更新了我的代码,在
glob
回调中包含
async
——如果没有它,我甚至不知道你是如何构建它的。这解决了它吗?我自己解决了它,并且
TypeError:route不是一个函数
现在是问题了吗