1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Fix with-custom-reverse-proxy example (#5064)

Fixes #5052.

Example was using babel 6.
This commit is contained in:
Daniel Reinoso 2018-08-30 18:32:19 -03:00 committed by Tim Neutkens
parent 4247a13ff1
commit 1924e3d868
4 changed files with 51 additions and 58 deletions

View file

@ -1,7 +1,5 @@
{
"presets": [
"next/babel",
"latest",
"stage-0"
"next/babel"
]
}

View file

@ -8,9 +8,6 @@
"react-dom": "16.2.0"
},
"devDependencies": {
"babel-preset-latest": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.24.1",
"cross-env": "^5.0.1",
"http-proxy-middleware": "^0.17.4"
},

View file

@ -1,50 +0,0 @@
/* eslint-disable no-console */
import express from 'express'
import next from '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 proxyMiddleware = require('http-proxy-middleware')
Object.keys(devProxy).forEach(function (context) {
server.use(proxyMiddleware(context, 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)
})

View file

@ -1,2 +1,50 @@
require('babel-register')
module.exports = require('./server.es6.js')
/* 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 proxyMiddleware = require('http-proxy-middleware')
Object.keys(devProxy).forEach(function (context) {
server.use(proxyMiddleware(context, 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)
})