kiliman / remix-express-vite-plugin

This package includes a Vite plugin to use in your Remix app. It configures an Express server for both development and production using TypeScript.
118 stars 6 forks source link

custom listen callback and expose server #3

Closed raulfdm closed 4 months ago

raulfdm commented 5 months ago

Hey @kiliman 👋

First, thanks for this project, really useful. The code is pretty straight, but it's nice having the community try to solve this once so we don't have to repeat the same solution repeatedly.

I'm trying to replace our custom express setup, but I encounter 2 limitations using this plugin (maybe due to lack of express knowledge).

The first is straightforward I guess. In our code, in the app.listen call back, we start a background process:

const server = app.listen(envVars.PORT, () => {
  logger.info('SERVER_STARTED', {
    msg: `Express server started on port http://localhost:${envVars.PORT}`,
  })

  startBackgroungWorker()
})

Is it possible to accept a listenerCallback to createExpressApp? It could either replace entirely this: https://github.com/kiliman/remix-express-vite-plugin/blob/00491fe27953c39b7affc8b61213656424f41ddd/src/express.ts#L75-L77

Or just be invoked (if sent) by passing the port.

The second thing is, in the previous code snippet, we use the server returned by app.listen to bind loggers when the server is shutdown:

Something like this:

// bind our shutdown fn
process.on('SIGTERM', gracefulShutdown)
process.on('SIGINT', gracefulShutdown)

function gracefulShutdown() {
  logger.info('SERVER_SHUTDOWN_SIGNAL', { msg: 'Server will shutdown' })

  // ensure if the server gets shut, we stop our background workers
  stopBackgroungWorker(() => {
    closeConnectionsAndExit()
  })
}

function closeConnectionsAndExit() {
  server.close(() => {
    logger.info('HTTP_SERVER_CLOSED', {
      msg: 'Server cannot accept more connections',
    })
    process.exit(0)
  })
}

So, my question is: can we have access to the server object?

Here I'm not quite sure about the right abstraction, specially because we only start listening when it's prod (out of vite): https://github.com/kiliman/remix-express-vite-plugin/blob/00491fe27953c39b7affc8b61213656424f41ddd/src/express.ts#L74-L78

I'm not sure if we could call configure later and have something like this:


let server: ServerEntry | undefined

 if (isProductionMode) {
    server = app.listen(port, () =>
      console.log(`Express server listening at http://localhost:${port}`),
    )
}

  // call custom configure function if provided
  configure?.(app, server)

(not sure if that would work)

Let me know what you think :)

Thanks

raulfdm commented 5 months ago

Found another issue:

https://github.com/kiliman/remix-express-vite-plugin/blob/00491fe27953c39b7affc8b61213656424f41ddd/src/vite.ts#L101

In our app we don't use this alias. :/

Instead we use $

kiliman commented 4 months ago

The updated v0.2.0 lets you own the server. In development, you configure the Vite server using the server config in vite.config.ts.

For production, you can create your own server via the createServer option.

kiliman commented 4 months ago

Closing issue. Please try the latest and see if this solves your issue.