Last active
May 5, 2022 07:27
-
-
Save stramel/57483a68559dca2c6471b3170a28042f to your computer and use it in GitHub Desktop.
NX Next.js Custom Server
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') | |
module.exports = async function customServer(app, settings, proxyConfig) { | |
const handle = app.getRequestHandler() | |
await app.prepare() | |
const server = express() | |
if (proxyConfig) { | |
const proxyMiddleware = require('http-proxy-middleware') | |
Object.keys(proxyConfig).forEach(context => { | |
server.use(proxyMiddleware(context, proxyConfig[context])) | |
}) | |
} | |
// Default catch-all handler to allow Next.js to handle all other routes | |
server.all('*', (req, res) => handle(req, res)) | |
server.listen(settings.port, settings.hostname) | |
} |
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 environmentTemplate = env => `https://${env}.mydomain.com` | |
const ENVIRONMENTS = { | |
local: 'http://localhost:3000', | |
dev: environmentTemplate('dev'), | |
qa: environmentTemplate('qa'), | |
} | |
const commonOptions = { | |
target: ENVIRONMENTS.dev, | |
changeOrigin: true, | |
ws: true, | |
onError: function(err) { | |
console.log(err.message) | |
}, | |
} | |
module.exports = { | |
'/proxy': commonOptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update your
serve
section of the app you want to modify inworkspace.json
to have these: