import { createServer } from "http";
import next from "next";

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(() => {
    createServer((req, res) => {
        if (!req.url) {
            return;
        }

        const full_href = `${process.env.HOST}${req.url}`;

        const url = new URL(full_href);

        if (url.pathname.match(/^\/api\//)) {
            return;
        }

        handle(req, res);
    }).listen(port);

    console.log(
        `> Server listening at http://localhost:${port} as ${
            dev ? "development" : process.env.NODE_ENV
        }`,
    );
});
