I'm struggling to get an inversify-express-utils service properly configured. I'm trying to use two controllers, but only one of them seems to be discovered by inversify.
With my current configuration, only helloController
seems to be registering. Requests to GET .../api/v1/hello
respond as expected with 200 OK. However, requests to GET .../api/health
respond with 404 NOT FOUND.
What am I doing wrong?
Here is my current configuration:
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!")
}
}
Figured it out.
The @controller() annotation doesn't seem to work with a root ('/') path.
Works after changing healthController.ts to the following