seanpmaxwell / overnight

TypeScript decorators for the ExpressJS Server.
MIT License
878 stars 40 forks source link

AddControllers not working #57

Closed RobertoGlez closed 4 years ago

RobertoGlez commented 4 years ago

I use "overnightjs" on my app structure, but controllers that create the routes not work, only on a specific computer, all routes give me 'Error Cannot GET /MyRoute', I have another computer where works fine literarly run the same code, is some a incompatibility?

Computer with Error: node: 12.16.3 typescript: 3.8.3 express: 4.17.1

joeykilpatrick commented 4 years ago

Version 1.7.0 has a bug that is causing this issue. Rolling back to 1.6.15 should fix the issue until version 1.7.1 is published. Additional discussion in #56

seanpmaxwell commented 4 years ago

Version 1.7.1 is published let me know if it works for you

woo-industries commented 4 years ago

Hi I'm having a similar issue. I have a trivial server that doesn't seem to work. The Controller is loaded (1.7.1 fixed that, thanks!) but now none of the routes work.

// controller.ts
@Controller("api/auth")
export class AuthController {
  @Get("test")
  private test() {
    console.log("It works")
  }
}
// server.ts
class APIServer extends Server {
  private readonly FRONT_END_MSG = "Welcome to the Roasted API."

  private readonly START_MSG = "API started on port: "

  constructor() {
    super(true)
    this.app.use(bodyParser.json())
    this.app.use(bodyParser.urlencoded({ extended: true }))

    // Initialize controllers
    this.setupControllers()
  }

  private setupControllers(): void {
    const AuthControllerInstance = new AuthController()
    super.addControllers([AuthControllerInstance])
  }

  public start(selectedPort?: number): void {
    const port = selectedPort || 3000
    this.app.get("*", (req: Request, res: Response) => {
      res.send(this.FRONT_END_MSG)
    })
    this.app.listen(port, () => {
      Logger.Imp(this.START_MSG + port)
    })
  }
}

export default APIServer

When I curl localhost:3000/api/auth/test I just get the generic front end message.

I'm on overnight 1.7.1 typescript 3.8.3 node 12.16.3

seanpmaxwell commented 4 years ago

Hey @joeykilpatrick sorry to call you out but your change on line 151 broke things:

for (const member in controller) {

This only loops though the instance-object properties and skips the prototype ones. I changed this part back to

        let members: any = Object.getOwnPropertyNames(controller);
        members = members.concat(Object.getOwnPropertyNames(prototype));
        members.forEach((member: any) => {

I updated Overnight to version 1.7.2 now

joeykilpatrick commented 4 years ago

I had just narrowed down the same issue. The tests ran properly, so it works when the typescript is run with ts-node, but I found that, as you said, it doesn't work when compiled and then run as a module. I'm still checking it out, seems like a weird behavior.

woo-industries commented 4 years ago

@seanpmaxwell this fixed it for me, thank you !

seanpmaxwell commented 4 years ago

Okay will close issue for now. But please reach out if something doesn't work.

RobertoGlez commented 4 years ago

update Overnight 1.7.2 works thanks so much!

seanpmaxwell commented 4 years ago

Please upgrade to 1.7.3 there was also a bug where 'this' was being undefined