server.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * More info about this file:
  3. * https://v2.quasar.dev/quasar-cli-vite/developing-ssr/ssr-webserver
  4. *
  5. * Runs in Node context.
  6. */
  7. /**
  8. * Make sure to yarn add / npm install (in your project root)
  9. * anything you import here (except for express and compression).
  10. */
  11. import express from 'express'
  12. import compression from 'compression'
  13. import {
  14. ssrClose,
  15. ssrCreate,
  16. ssrListen,
  17. ssrRenderPreloadTag,
  18. ssrServeStaticContent
  19. } from 'quasar/wrappers'
  20. /**
  21. * Create your webserver and return its instance.
  22. * If needed, prepare your webserver to receive
  23. * connect-like middlewares.
  24. *
  25. * Should NOT be async!
  26. */
  27. export const create = ssrCreate((/* { ... } */) => {
  28. const app = express()
  29. // attackers can use this header to detect apps running Express
  30. // and then launch specifically-targeted attacks
  31. app.disable('x-powered-by')
  32. // place here any middlewares that
  33. // absolutely need to run before anything else
  34. if (process.env.PROD) {
  35. app.use(compression())
  36. }
  37. return app
  38. })
  39. /**
  40. * You need to make the server listen to the indicated port
  41. * and return the listening instance or whatever you need to
  42. * close the server with.
  43. *
  44. * The "listenResult" param for the "close()" definition below
  45. * is what you return here.
  46. *
  47. * For production, you can instead export your
  48. * handler for serverless use or whatever else fits your needs.
  49. */
  50. export const listen = ssrListen(async ({ app, port, isReady }) => {
  51. await isReady()
  52. return app.listen(port, () => {
  53. if (process.env.PROD) {
  54. console.log('Server listening at port ' + port)
  55. }
  56. })
  57. })
  58. /**
  59. * Should close the server and free up any resources.
  60. * Will be used on development only when the server needs
  61. * to be rebooted.
  62. *
  63. * Should you need the result of the "listen()" call above,
  64. * you can use the "listenResult" param.
  65. *
  66. * Can be async.
  67. */
  68. export const close = ssrClose(({ listenResult }) => {
  69. return listenResult.close()
  70. })
  71. const maxAge = process.env.DEV
  72. ? 0
  73. : 1000 * 60 * 60 * 24 * 30
  74. /**
  75. * Should return middleware that serves the indicated path
  76. * with static content.
  77. */
  78. export const serveStaticContent = ssrServeStaticContent((path, opts) => {
  79. return express.static(path, {
  80. maxAge,
  81. ...opts
  82. })
  83. })
  84. const jsRE = /\.js$/
  85. const cssRE = /\.css$/
  86. const woffRE = /\.woff$/
  87. const woff2RE = /\.woff2$/
  88. const gifRE = /\.gif$/
  89. const jpgRE = /\.jpe?g$/
  90. const pngRE = /\.png$/
  91. /**
  92. * Should return a String with HTML output
  93. * (if any) for preloading indicated file
  94. */
  95. export const renderPreloadTag = ssrRenderPreloadTag((file) => {
  96. if (jsRE.test(file) === true) {
  97. return `<link rel="modulepreload" href="${file}" crossorigin>`
  98. }
  99. if (cssRE.test(file) === true) {
  100. return `<link rel="stylesheet" href="${file}">`
  101. }
  102. if (woffRE.test(file) === true) {
  103. return `<link rel="preload" href="${file}" as="font" type="font/woff" crossorigin>`
  104. }
  105. if (woff2RE.test(file) === true) {
  106. return `<link rel="preload" href="${file}" as="font" type="font/woff2" crossorigin>`
  107. }
  108. if (gifRE.test(file) === true) {
  109. return `<link rel="preload" href="${file}" as="image" type="image/gif">`
  110. }
  111. if (jpgRE.test(file) === true) {
  112. return `<link rel="preload" href="${file}" as="image" type="image/jpeg">`
  113. }
  114. if (pngRE.test(file) === true) {
  115. return `<link rel="preload" href="${file}" as="image" type="image/png">`
  116. }
  117. return ''
  118. })