index.ts 1.2 KB
Newer Older
1 2 3 4
import path from 'path';
import express from 'express';
import next from 'next';
import {setConfig} from 'next/config';
P
Peter Pan 已提交
5
import {nextI18NextMiddleware} from '../utils/i18next/middlewares';
6
import nextI18next from '../utils/i18n';
7 8 9 10 11 12
import config from '../next.config';

const isDev = process.env.NODE_ENV !== 'production';

setConfig(config);

P
Peter Pan 已提交
13
const host = process.env.HOST || 'localhost';
14
const port = process.env.PORT || 8999;
P
Peter Pan 已提交
15
const proxy = process.env.PROXY;
16 17 18 19 20 21 22
const app = next({dev: isDev, conf: config});
const handle = app.getRequestHandler();

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

P
Peter Pan 已提交
23 24 25 26
    if (proxy) {
        const {createProxyMiddleware} = await import('http-proxy-middleware');
        server.use(config.env.API_URL, createProxyMiddleware({target: proxy, changeOrigin: true}));
    } else if (isDev) {
27
        const {default: mock} = await import('../utils/mock');
28 29 30
        server.use(config.env.API_URL, mock({path: path.resolve(__dirname, '../mock')}));
    }

31 32 33
    await nextI18next.initPromise;
    server.use(nextI18NextMiddleware(nextI18next));

34 35 36
    server.get('*', (req, res) => handle(req, res));

    server.listen(port);
P
Peter Pan 已提交
37
    console.log(`> Ready on http://${host}:${port}`); // eslint-disable-line no-console
38
})();