Commit 18d31f60 authored by Arunoda Susiripala's avatar Arunoda Susiripala

Make this proxy rules similar to path-alias.

parent 957b2b38
...@@ -38,7 +38,7 @@ if (argv.help) { ...@@ -38,7 +38,7 @@ if (argv.help) {
} }
const rulesFilePath = resolve(process.cwd(), argv['rules-file']) const rulesFilePath = resolve(process.cwd(), argv['rules-file'])
const rules = require(rulesFilePath) const { rules } = require(rulesFilePath)
const proxy = buildProxy(rules) const proxy = buildProxy(rules)
proxy.listen(argv.port, argv.hostname, (err) => { proxy.listen(argv.port, argv.hostname, (err) => {
if (err) { if (err) {
......
[ {
{ "rules": [
"pathname": "/blog(/**)", {"pathname": "/blog(/**)", "method":["GET", "POST", "OPTIONS"], "dest": "http://localhost:5000"},
"zone": { "name": "blog", "url": "http://localhost:5000" } {"dest": "http://localhost:4000"}
}, ]
{ }
"pathname": "/**", \ No newline at end of file
"zone": { "name": "home", "url": "http://localhost:4000" }
}
]
\ No newline at end of file
...@@ -2,28 +2,26 @@ import micro from 'micro' ...@@ -2,28 +2,26 @@ import micro from 'micro'
import UrlPattern from 'url-pattern' import UrlPattern from 'url-pattern'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { resolve } from 'url' import { resolve } from 'url'
import { isInternalUrl } from './utils'
export default (rules) => { export default (rules) => {
const patterns = rules.map(({ pathname, zone }) => ({ const patterns = rules.map(({ pathname, dest, method }) => {
pathname: new UrlPattern(pathname), const methods = method ? method.reduce((final, c) => {
zone final[c.toLowerCase()] = true
})) return final
}, {}) : null
return micro(async (req, res) => {
// Proxy internal urls (like /_next/**) return {
if (isInternalUrl(req.url)) { pathname: new UrlPattern(pathname || '/**'),
console.error(`Trying to access an internal URL via the proxy: ${req.url} dest,
Did you configure "assetPrefix" on all the zones?`) methods
res.writeHead(404)
res.end('404 - Not Found')
return
} }
})
return micro(async (req, res) => {
// Find a matching zone for the request and proxy it // Find a matching zone for the request and proxy it
for (const { pathname, zone } of patterns) { for (const { pathname, dest, methods } of patterns) {
if (pathname.match(req.url)) { if (pathname.match(req.url) && (!methods || methods[req.method.toLowerCase()])) {
await proxyRequest(req, res, zone) await proxyRequest(req, res, dest)
return return
} }
} }
...@@ -33,8 +31,8 @@ Did you configure "assetPrefix" on all the zones?`) ...@@ -33,8 +31,8 @@ Did you configure "assetPrefix" on all the zones?`)
}) })
} }
async function proxyRequest (req, res, zone) { async function proxyRequest (req, res, dest) {
const newUrl = resolve(zone.url, req.url) const newUrl = resolve(dest, req.url)
const proxyRes = await fetch(newUrl, { const proxyRes = await fetch(newUrl, {
method: 'GET' method: 'GET'
}) })
......
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