index.js 1.72 KB
Newer Older
Julien Benchetrit's avatar
Julien Benchetrit committed
1
/* eslint consistent-return:0 import/order:0 */
2 3
// 先挂环境变量
require('./env');
Julien Benchetrit's avatar
Julien Benchetrit committed
4 5 6 7 8 9
const express = require('express');
const logger = require('./logger');
const argv = require('./argv');
const port = require('./port');
const setup = require('./middlewares/frontendMiddleware');
const isDev = process.env.NODE_ENV !== 'production';
10 11 12 13
// const ngrok =
//   (isDev && process.env.ENABLE_TUNNEL) || argv.tunnel
//     ? require('ngrok')
//     : false;
Julien Benchetrit's avatar
Julien Benchetrit committed
14 15 16 17 18 19 20 21
const { resolve } = require('path');
const app = express();

// If you need a backend, e.g. an API, add your custom backend-specific middleware here
// app.use('/api', myApi);

// In production we need to pass these values in instead of relying on webpack
setup(app, {
22 23
  outputPath: resolve(process.cwd(), 'civmanage'),
  publicPath: process.env.NODE_ENV === 'production' ?'/civmanage': '/',
Julien Benchetrit's avatar
Julien Benchetrit committed
24 25 26 27 28
});

// get the intended host and port number, use localhost and port 3000 if not provided
const customHost = argv.host || process.env.HOST;
const host = customHost || null; // Let http.Server use its default IPv6/4 host
29
const prettyHost = (host !== 'origin' && customHost) || 'localhost';
Julien Benchetrit's avatar
Julien Benchetrit committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// use the gzipped bundle
app.get('*.js', (req, res, next) => {
  req.url = req.url + '.gz'; // eslint-disable-line
  res.set('Content-Encoding', 'gzip');
  next();
});

// Start your app.
app.listen(port, host, async err => {
  if (err) {
    return logger.error(err.message);
  }

  // Connect to ngrok in dev mode
45 46 47 48 49 50 51 52 53 54 55 56
  // if (ngrok) {
  //   let url;
  //   try {
  //     url = await ngrok.connect(port);
  //   } catch (e) {
  //     return logger.error(e);
  //   }
  //   logger.appStarted(port, prettyHost, url);
  // } else {
  //   logger.appStarted(port, prettyHost);
  // }
  logger.appStarted(port, prettyHost);
Julien Benchetrit's avatar
Julien Benchetrit committed
57
});