Skip to content

Instantly share code, notes, and snippets.

@stramel
Last active May 5, 2022 07:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stramel/57483a68559dca2c6471b3170a28042f to your computer and use it in GitHub Desktop.
Save stramel/57483a68559dca2c6471b3170a28042f to your computer and use it in GitHub Desktop.
NX Next.js Custom Server
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)
}
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
}
@stramel
Copy link
Author

stramel commented Feb 20, 2020

Update your serve section of the app you want to modify in workspace.json to have these:

+  "proxyConfig": "apps/<my-app>/setupProxy.js",
+  "customServerPath": "server.js"

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