index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const express = require("express")
  2. const { GracefulShutdownServer } = require("medusa-core-utils")
  3. const loaders = require("@medusajs/medusa/dist/loaders/index").default
  4. ;(async() => {
  5. async function start() {
  6. const app = express()
  7. const directory = process.cwd()
  8. try {
  9. const { dbConnection, container } = await loaders({ directory, expressApp: app })
  10. const configModule = container.resolve("configModule")
  11. const port = configModule.projectConfig.port ?? 9000
  12. const server = GracefulShutdownServer.create(
  13. app.listen(port, (err) => {
  14. if (err) {
  15. return
  16. }
  17. console.log(`Server is ready on port: ${port}`)
  18. })
  19. )
  20. // Handle graceful shutdown
  21. const gracefulShutDown = () => {
  22. server
  23. .shutdown()
  24. .then(() => {
  25. console.info("Gracefully stopping the server.")
  26. process.exit(0)
  27. })
  28. .catch((e) => {
  29. console.error("Error received when shutting down the server.", e)
  30. process.exit(1)
  31. })
  32. }
  33. process.on("SIGTERM", gracefulShutDown)
  34. process.on("SIGINT", gracefulShutDown)
  35. } catch (err) {
  36. console.error("Error starting server", err)
  37. process.exit(1)
  38. }
  39. }
  40. await start()
  41. })()