Typescript & Express: Export several routes from single index.ts file to prefix with ''/api/v1"

时间:2022-09-25 11:58:39

I am trying to tidy up my routes. I would like to have 1 file,index.ts, to export all my routes from. I have seen something similar done in plain JavaScript but not sure on the syntax in typescript.

我想整理我的路线。我想有一个文件index.ts来导出我的所有路由。我在普通的JavaScript中看到过类似的东西但是对typescript中的语法不确定。

The error I get is: TypeError: Router.use() requires a middleware function but got a Object

(old but works)BaseRoutes.ts

(旧但有效)BaseRoutes.ts

import * as express from 'express';
import {PostRoutes} from '../PostRoutes';
import {CspRoutes} from '../CspRoutes';
import {CustomerPreferenceRoutes} from '../CustomerPreferenceRoutes';
import { SalesOrderRoutes } from '../SalesOrderRoutes';
let app = express();
const BASE_API: string = '/api/v2';
class BaseRoutes{
    get routes(){
        app.use(BASE_API, new PostRoutes().routes);
        app.use(BASE_API, new CspRoutes().routes);
        app.use(BASE_API, new CustomerPreferenceRoutes().routes);
        app.use(BASE_API, new SalesOrderRoutes().routes);
        return app;
    }
}
export {BaseRoutes}

(new does not work)BaseRoutes.ts

(新的不起作用)BaseRoutes.ts

import * as express from 'express';

let routes = require('../index');
let app = express();
const BASE_API: string = '/api/v2';

class BaseRoutes{
    get routes(){
        app.use(BASE_API,routes);
        return app;
    }
}
export {BaseRoutes}

PostRoutes.ts

PostRoutes.ts

import * as express from 'express';
import {PostController} from '../../controllers/PostController'

let router = express.Router();

class PostRoutes{
    private _postController:PostController;
    constructor(){
        this._postController = new PostController();
    }
    get routes(){
        let controller = this._postController
        router.get('/posts',controller.retrieve)
        router.get('/posts/:_id',controller.findById)
        return router;
    }
}
export{PostRoutes};

index.ts

index.ts

export * from './PostRoutes';
export * from './SalesOrderRoutes';

1 个解决方案

#1


0  

It is because you messed up your imports.

这是因为你搞砸了你的进口。

export * from './PostRoutes';
export * from './SalesOrderRoutes';

You are re-exporting all the exports from PostRoutes and SalesOrderRoutes etc. , so when you finally import it in BaseRoutes , you actually import all the constructors you exported in each route.

您正在从PostRoutes和SalesOrderRoutes等重新导出所有导出,因此当您最终在BaseRoutes中导入它时,实际上导入了在每个路径中导出的所有构造函数。

What you want to do is:

你想要做的是:

import { PostRoutes, SalesOrderRoutes } from '../index';

And then use() each route explicitly.

然后明确地使用()每个路由。

Edit: You can read more about Typescript modules and Javascript modules

编辑:您可以阅读有关Typescript模块和Javascript模块的更多信息


#1


0  

It is because you messed up your imports.

这是因为你搞砸了你的进口。

export * from './PostRoutes';
export * from './SalesOrderRoutes';

You are re-exporting all the exports from PostRoutes and SalesOrderRoutes etc. , so when you finally import it in BaseRoutes , you actually import all the constructors you exported in each route.

您正在从PostRoutes和SalesOrderRoutes等重新导出所有导出,因此当您最终在BaseRoutes中导入它时,实际上导入了在每个路径中导出的所有构造函数。

What you want to do is:

你想要做的是:

import { PostRoutes, SalesOrderRoutes } from '../index';

And then use() each route explicitly.

然后明确地使用()每个路由。

Edit: You can read more about Typescript modules and Javascript modules

编辑:您可以阅读有关Typescript模块和Javascript模块的更多信息