我正在努力获取正确配置的inversify-express-utils服务。我正在尝试使用两个控制器,但是似乎其中只有一个被inversify发现。
在我当前的配置下,似乎只有helloController
正在注册。对GET .../api/v1/hello
的请求按预期方式响应200 OK。但是,对GET .../api/health
的请求将以404 Not FOUND响应。
我在做什么错了?
这是我当前的配置:
src
│
└──api
│ index.ts
│
└──controller
│ index.ts
│ healthController.ts
│
└──v1
│ index.ts
│ helloController.ts
src / api / index.ts
import "reflect-metadata"
import {Container} from "inversify"
import {InversifyExpressServer} from "inversify-express-utils"
import "./api/controller"
const inversifyContainer = new Container()
const server = new InversifyExpressServer(inversifyContainer)
const serverInstance = server.build()
serverInstance.listen(3000)
src / api / controller / index.ts
export * from "./healthController"
export * from "./v1/"
src / api / controller / healthController.ts
import {Request, Response} from "express"
import {BaseHttpController, controller, httpGet} from "inversify-express-utils"
@controller('/')
export class HealthController extends BaseHttpController {
constructor(){ super() }
@httpGet('/health')
private health(req: Request, resp: Response) {
resp.json({uptime: process.uptime()})
}
}
src / api / controller / v1 / index.ts
export * from "./helloController"
src / api / controller / v1 / helloController.ts
import {Request, Response} from "express"
import {BaseHttpController, controller, httpGet} from "inversify-express-utils"
@controller('/v1/hello')
export class helloController extends BaseHttpController {
@httpGet('/')
private hello(req: Request, resp: Response) {
resp.send("Hello, World!")
}
}
答案 0 :(得分:0)
想通了。
@controller()注释似乎不适用于根('/')路径。
将 healthController.ts 更改为以下内容
后即可工作import {Request, Response} from "express"
import {BaseHttpController, controller, httpGet} from "inversify-express-utils"
@controller('/health')
export class HealthController extends BaseHttpController {
@httpGet('/')
private health(req: Request, resp: Response) {
resp.json({uptime: process.uptime()})
}
}
答案 1 :(得分:0)
它运作良好,但您不应使用斜杠作为根路径:
import {Request, Response} from "express"
import {BaseHttpController, controller, httpGet} from "inversify-express-utils"
@controller('')
export class HealthController extends BaseHttpController {
@httpGet('/health')
private health(req: Request, resp: Response) {
resp.json({uptime: process.uptime()})
}
}