index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* eslint consistent-return:0 */
  2. const express = require('express')
  3. const logger = require('./logger')
  4. const argv = require('./argv')
  5. const port = require('./port')
  6. const setup = require('./middlewares/frontendMiddleware')
  7. const { resolve } = require('path')
  8. const app = express()
  9. // If you need a backend, e.g. an API, add your custom backend-specific middleware here
  10. // app.use('/api', myApi);
  11. // In production we need to pass these values in instead of relying on webpack
  12. setup(app, {
  13. outputPath: resolve(process.cwd(), 'build'),
  14. publicPath: '/'
  15. })
  16. // get the intended host and port number, use localhost and port 3000 if not provided
  17. const customHost = argv.host || process.env.HOST
  18. const host = customHost || null // Let http.Server use its default IPv6/4 host
  19. const prettyHost = customHost || 'localhost'
  20. // use the gzipped bundle
  21. app.get('*.js', (req, res, next) => {
  22. req.url = req.url + '.gz' // eslint-disable-line
  23. res.set('Content-Encoding', 'gzip')
  24. next()
  25. })
  26. // Start your app.
  27. app.listen(port, host, async err => {
  28. if (err) {
  29. return logger.error(err.message)
  30. }
  31. logger.appStarted(port, prettyHost)
  32. })