Skip to content

Instantly share code, notes, and snippets.

@tiptopcoder
Last active January 30, 2022 18:29
Show Gist options
  • Save tiptopcoder/442c2d5d09062df5700f7af6ded58792 to your computer and use it in GitHub Desktop.
Save tiptopcoder/442c2d5d09062df5700f7af6ded58792 to your computer and use it in GitHub Desktop.
Next - Express Custom Server in Typescript
import express, { Request, Response } from "express";
import next from "next";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const port = process.env.PORT || 3000;
(async () => {
try {
await app.prepare();
const server = express();
server.all("*", (req: Request, res: Response) => {
return handle(req, res);
});
server.listen(port, (err?: any) => {
if (err) throw err;
console.log(`> Ready on localhost:${port} - env ${process.env.NODE_ENV}`);
});
} catch (e) {
console.error(e);
process.exit(1);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment