rsnext/examples/with-custom-reverse-proxy/server.js
Mathis Chenuet 16d2235882
Update proxy example to http-proxy-middleware ^1 (#13507)
The current example doesn't work if one try to reproduce it with a recent version of http-proxy-middleware.
2020-05-28 21:00:58 +00:00

50 lines
1.2 KiB
JavaScript

/* eslint-disable no-console */
const express = require('express')
const next = require('next')
const devProxy = {
'/api': {
target: 'https://swapi.co/api/',
pathRewrite: { '^/api': '/' },
changeOrigin: true,
},
}
const port = parseInt(process.env.PORT, 10) || 3000
const env = process.env.NODE_ENV
const dev = env !== 'production'
const app = next({
dir: '.', // base directory where everything is, could move to src later
dev,
})
const handle = app.getRequestHandler()
let server
app
.prepare()
.then(() => {
server = express()
// Set up the proxy.
if (dev && devProxy) {
const { createProxyMiddleware } = require('http-proxy-middleware')
Object.keys(devProxy).forEach(function (context) {
server.use(context, createProxyMiddleware(devProxy[context]))
})
}
// 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)
})