Skip to content
Snippets Groups Projects
index.ts 1.05 KiB
import express, { Request, Response } from 'express';
import next from 'next';
import { ParsedUrlQuery } from 'querystring';

const PORT = parseInt(process.env.PORT || '3000', 10);
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

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

    // Route for the main page (`/`)
    server.get('/', (req: Request, res: Response) => {
        // idk
        const query: ParsedUrlQuery = Object.keys(req.query).reduce((acc, key) =>
        {
            acc[key] = req.query[key] as string | string[] | undefined;
            return acc;
        }, {} as ParsedUrlQuery);

        // Render the Next.js main page
        return app.render(req, res, '/', query);
    });

    // Route for other pages (fallback to Next.js default handler)
    server.all('*', (req: Request, res: Response) => {
        return handle(req, res);
    });

    // Start the server
    server.listen(PORT, () => {
        console.log(`Server is running on PORT ${PORT}`);
    });
})