{"size": 1798, "ext": "js", "max_stars_repo_path": "src/server/index.js", "max_stars_repo_name": "rt-cff/next-starter", "max_stars_repo_stars_event_min_datetime": "2020-08-06T10:30:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-01T16:00:11.000Z", "max_issues_repo_path": "src/server/index.js", "max_issues_repo_name": "rt-cff/next-starter", "max_issues_repo_issues_event_min_datetime": "2019-10-08T06:07:58.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-24T18:25:15.000Z", "max_forks_repo_path": "src/server/index.js", "max_forks_repo_name": "rt-cff/next-starter", "max_forks_repo_forks_event_min_datetime": "2020-08-15T10:58:17.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-15T10:58:17.000Z"}
{"max_stars_count": 4, "max_issues_count": 87, "max_forks_count": 1, "avg_line_length": 26.4411764706, "max_line_length": 99, "alphanum_fraction": 0.6256952169}
/* eslint-disable @typescript-eslint/no-var-requires */
const express = require('express');
const next = require('next');
const path = require('path');
const devProxy = require('./proxy');

const PORT = process.env.PORT || 3000;

const app = next({
  dev: process.env.NODE_ENV !== 'production',
});

const routes = require('./routes');
const handler = routes.getRequestHandler(app);

app.prepare().then(() => {
  const server = express();

  app.setAssetPrefix(process.env.STATIC_PATH);

  server.use(express.static(path.join(__dirname, '../../static')));

  if (process.env.PROXY_MODE === 'local') {
    const proxyMiddleware = require('http-proxy-middleware');
    Object.keys(devProxy).forEach(function(context) {
      server.use(proxyMiddleware(context, devProxy[context]));
    });
  }

  server.get('*', async (req, res) => {
    return handler(req, res);
  });

  const instance = server.listen(PORT, err => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${PORT}`);

    // NOTE: we enabled the cluster mode of pm2.
    //       http://pm2.keymetrics.io/docs/usage/cluster-mode/#cluster-mode
    //       it required slave processes to notify master process if it's ready to accept requests.
    if (process.send) {
      console.log(`process(${process.pid}) is ready`);
      process.send('ready');
    }
  });

  const shutdown = async () => {
    console.log('server is closing now');

    instance.close(error => {
      if (error) {
        console.error('server closed with error', error);
        process.exit(1);
      }

      console.log('server closed');
      process.exit(0);
    });
  };

  process.on('SIGINT', async () => {
    console.error(`SIGINT ${process.pid}`);
    shutdown();
  });
});

/* eslint-enable @typescript-eslint/no-var-requires */
