【发布时间】:2019-07-14 09:50:04
【问题描述】:
我正在尝试使用 Express 路由器和 Typescript 中的类来组织我的路线。这是我到目前为止所尝试的。 index.ts 文件应该引用 notes.ts 文件中的 Notes 类,该文件通过私有方法公开端点。
例如,我有一个文件index.ts
import * as express from "express";
import rootRoutes from "./root";
import { Notes } from "./notes";
import accountRoutes from "./account";
const router: express.Router = express.Router();
router.use("/", rootRoutes);
router.use("/account", accountRoutes);
router.use("/notes", Notes.routes);
export = router;
还有另一个文件notes.ts:
import express, { Router, Request, Response, NextFunction } from "express";
const router: express.Router = express.Router();
export class Notes {
constructor() {
this.routes();
}
private routes(): void {
//Get notes
router
.route("/")
.get((req: Request, res: Response, next: NextFunction) => {
res.send("notes");
});
}
}
我在这里错过了什么?
【问题讨论】:
标签: node.js typescript express