如何解决没有执行路由的快速装饰器类
我一直在为快速控制器编写自定义装饰器。装饰器类没有被执行。我检查了 bundle.js 和装饰器代码被捆绑在 bundle.js 中
@controller("/api/v1/portfolios")
class PortfolioController {
@get("")
async getPortfolios(req: Request,res: Response) {
const portfolios = await Portfolio.find({});
return res.json(portfolios);
}}
我在 app.js 中导入了这个
const server = express();
import("./controllers/portfolios");
server.use(AppRouter.getInstance());
路由器是一个单例对象。
export class AppRouter {
private static instance: express.Router;
static getInstance(): express.Router {
if (!AppRouter.instance) {
AppRouter.instance = express.Router();
}
return AppRouter.instance;
}
}
这是控制器装饰器
export function controller(routePrefix: string) {
return function (target: Function) {
const router = AppRouter.getInstance();
for (let key in target.prototype) {
const routeHandler = target.prototype[key]; //getLogin
const path = Reflect.getMetadata(
MetadataKeys.Path,target.prototype,key
);
const method: Methods = Reflect.getMetadata(
MetadataKeys.Method,key
);
const middlewares =
Reflect.getMetadata(MetadataKeys.Middleware,key) ||
[];
if (path) {
router[method](
`${routePrefix}${path}`,...middlewares,// validator,routeHandler
);
}
}
};
}
这是 webpack.config.json
const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const nodeWebExternals = require("webpack-node-externals");
module.exports = {
entry: ["regenerator-runtime/runtime","./src/index.ts"],mode: "development",target: "node",output: {
filename: "bundle.js",path: path.join(__dirname,"dist"),},module: {
rules: [{ test: /\.ts$/,loader: "ts-loader",exclude: /node_modules/ }],resolve: {
extensions: [".ts",".js",".json"],modules: [path.resolve(__dirname,"./src"),"node_modules"],externals: [nodeWebExternals()],plugins: [new CleanWebpackPlugin()],};
我收到此错误:
Cannot GET /api/v1/blogs
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。