Last active
February 13, 2024 14:00
-
-
Save bonface221/941506540fe75f797933e3899aa47dfb to your computer and use it in GitHub Desktop.
Next.js Deployment with Express: !! -> install express first using yarn add express or npm install express and then create a file called server.js-(server js should be at the root of the application.) and add the following-
This file contains hidden or 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
const express = require("express"); | |
const next = require("next"); | |
const dev = process.env.NODE_ENV !== "production"; // make sure to have the NODE_ENV on the env | |
const app = next({ dev }); | |
const handle = app.getRequestHandler(); | |
app.prepare().then(() => { | |
const server = express(); | |
server.get("*", (req, res) => { | |
return handle(req, res); | |
}); | |
const port = process.env.PORT || 3000; | |
server.listen(port, (err) => { | |
if (err) throw err; | |
console.log(`> Ready on http://localhost:${port}`); | |
}); | |
}); | |
// run yarn build or next build to build for production |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
package.json must contain express js then the app should be build