Last active
January 30, 2022 18:29
-
-
Save tiptopcoder/442c2d5d09062df5700f7af6ded58792 to your computer and use it in GitHub Desktop.
Next - Express Custom Server in Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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