gajus / http-terminator

Gracefully terminates HTTP(S) server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: TypeError: Cannot read property 'on' of undefined

mrmartineau opened this issue · comments

It is entirely possible that I may have misconfigured the setup for this package but just in case I haven't please could you review my code to ensure that I haven't done so..

nextApp.prepare().then(() => {
  const expressApp = express()
  const expressServer = expressApp.listen(3000, (error: Error) => {
    if (error) {
      throw error
    }
    console.log(`App ready on http://localhost:3000`)
  })

  const httpTerminator = createHttpTerminator({
    expressServer,
  })

  httpTerminator.terminate().then(() => {
    console.log('Express server terminated')
  })
})

I am creating a next.js app, which when used with Express uses config like this. See here for a more complete example if you're interested.

When using this, I always receive this error when the express server starts:

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
TypeError: Cannot read property 'on' of undefined
    at createInternalHttpTerminator (/Users/myUsername/projectName/node_modules/http-terminator/src/factories/createInternalHttpTerminator.js:32:10)
    at Object.createHttpTerminator (/Users/myUsername/projectName/node_modules/http-terminator/src/factories/createHttpTerminator.js:10:26)
    at /Users/myUsername/projectName/src/server.ts:161:24

Line 161 is this const httpTerminator = createHttpTerminator({

Can you see anything obvious that I'm missing or have misconfigured?

It should be:

const httpTerminator = createHttpTerminator({
  server: expressServer,
})

🤦‍♂ i'm such an idiot.. Thanks for your help

@mrmartineau I ran into this one as well; I blame it on the missing types 😄