Skip to content

Instantly share code, notes, and snippets.

@bonface221
Last active February 13, 2024 14:00
Show Gist options
  • Save bonface221/941506540fe75f797933e3899aa47dfb to your computer and use it in GitHub Desktop.
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-
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
@bonface221
Copy link
Author

image

package.json must contain express js then the app should be build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment