index.js 1.3 KB

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