-
-
Save PsyGik/580720e6087e77bb50c0a6e80f3a8ece to your computer and use it in GitHub Desktop.
NX Next.js Custom Server
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
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 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