Commit 6a9b3412 authored by Arunoda Susiripala's avatar Arunoda Susiripala

Use micro-proxy instead of 'next proxy'.

Remove all the proxy related code from Next.js
parent 18d31f60
......@@ -23,7 +23,6 @@ const commands = new Set([
'build',
'start',
'export',
'proxy',
defaultCommand
])
......
#!/usr/bin/env node
import parseArgs from 'minimist'
import buildProxy from '../server/proxy'
import { resolve } from 'path'
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
H: 'hostname',
p: 'port',
r: 'rules-file'
},
boolean: ['h'],
string: ['H', 'r'],
default: { p: 9000, H: 'localhost' }
})
if (!argv['rules-file']) {
console.error(`> Start the proxy with --rules-file (-r) option\n`)
process.exit(1)
}
if (argv.help) {
console.log(`
Description
Start a proxy for different Next.js zones.
Usage
$ next proxy -r <rules.js> -p <port>
Options
--rules-file, -r A .json file which contains rules for the proxy
--port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application
--help, -h Displays this message
`)
process.exit(0)
}
const rulesFilePath = resolve(process.cwd(), argv['rules-file'])
const { rules } = require(rulesFilePath)
const proxy = buildProxy(rules)
proxy.listen(argv.port, argv.hostname, (err) => {
if (err) {
throw err
}
console.log(`> Ready on http://${argv.hostname}:${argv.port}`)
})
......@@ -8,8 +8,8 @@ export default () => (
<div>
<Header />
<p>This is our homepage</p>
<div><Link href='/blog' as='/blog'><a>Blog</a></Link></div>
<div><Link href='/about' as='/about'><a>About us</a></Link></div>
<div><Link href='/blog'><a>Blog</a></Link></div>
<div><Link href='/about'><a>About us</a></Link></div>
<img width={200} src={asset('/nextjs.png')} />
</div>
)
......@@ -8,10 +8,11 @@
"blog": "next blog -p 5000",
"blog-build": "next build blog",
"blog-start": "next start blog -p 5000",
"proxy": "next proxy -r rules.json"
"proxy": "micro-proxy -r rules.json"
},
"dependencies": {
"next": "latest",
"micro-proxy": "^1.0.0",
"next": "^4.1.4 ",
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
......
{
"rules": [
{"pathname": "/blog(/**)", "method":["GET", "POST", "OPTIONS"], "dest": "http://localhost:5000"},
{"dest": "http://localhost:4000"}
{"pathname": "/blog", "method":["GET", "POST", "OPTIONS"], "dest": "http://localhost:5000"},
{"pathname": "/**", "dest": "http://localhost:4000"}
]
}
\ No newline at end of file
}
import micro from 'micro'
import UrlPattern from 'url-pattern'
import fetch from 'node-fetch'
import { resolve } from 'url'
export default (rules) => {
const patterns = rules.map(({ pathname, dest, method }) => {
const methods = method ? method.reduce((final, c) => {
final[c.toLowerCase()] = true
return final
}, {}) : null
return {
pathname: new UrlPattern(pathname || '/**'),
dest,
methods
}
})
return micro(async (req, res) => {
// Find a matching zone for the request and proxy it
for (const { pathname, dest, methods } of patterns) {
if (pathname.match(req.url) && (!methods || methods[req.method.toLowerCase()])) {
await proxyRequest(req, res, dest)
return
}
}
res.writeHead(404)
res.end('404 - Not Found')
})
}
async function proxyRequest (req, res, dest) {
const newUrl = resolve(dest, req.url)
const proxyRes = await fetch(newUrl, {
method: 'GET'
})
// Forward headers
const headers = proxyRes.headers.raw()
for (const key of Object.keys(headers)) {
res.setHeader(key, headers[key])
}
// Stream the proxy response
proxyRes.body.pipe(res)
proxyRes.body.on('error', (err) => {
console.error(`Error on proxying url: ${newUrl}`)
console.error(err.stack)
res.end()
})
req.on('abort', () => {
proxyRes.body.destroy()
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment