index.ts 942 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import path from 'path';
import express from 'express';
import next from 'next';
import {setConfig} from 'next/config';
import nextI18NextMiddleware from 'next-i18next/middleware';
import config from '../next.config';
import nextI18next from '../utils/i18n';
import mock from '../utils/mock';

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

setConfig(config);

const port = process.env.PORT || 8999;
const app = next({dev: isDev, conf: config});
const handle = app.getRequestHandler();

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

    if (isDev) {
        server.use(config.env.API_URL, mock({path: path.resolve(__dirname, '../mock')}));
    }

    await nextI18next.initPromise;
    server.use(nextI18NextMiddleware(nextI18next));

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

    server.listen(port);
    console.log(`> Ready on http://localhost:${port}`); // eslint-disable-line no-console
})();