Last active
September 4, 2023 18:33
-
-
Save kristersd/db4c2d080325455854be979206990fb8 to your computer and use it in GitHub Desktop.
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 port = parseInt(process.env.PORT, 10) || 3000; | |
const env = process.env.NODE_ENV; | |
const dev = env !== 'production'; | |
const app = next({ | |
dir: '.', | |
dev | |
}); | |
const handle = app.getRequestHandler(); | |
const server = express(); | |
app.prepare() | |
.then(() => { | |
if (dev) { | |
const proxyMiddleware = require('http-proxy-middleware'); | |
server.use( | |
proxyMiddleware('/api', { | |
target: 'http://backend:8000', | |
changeOrigin: true | |
}) | |
); | |
} | |
// Default catch-all handler to allow Next.js to handle all other routes | |
server.all('*', (req, res) => handle(req, res)); | |
server.listen(port, err => { | |
if (err) { | |
throw err; | |
} | |
console.log(`> Ready on port ${port} [${env}]`); | |
}); | |
}) | |
.catch(err => { | |
console.log('An error occurred, unable to start the server'); | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment