Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
18d31f60
Commit
18d31f60
authored
Nov 24, 2017
by
Arunoda Susiripala
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make this proxy rules similar to path-alias.
parent
957b2b38
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
30 deletions
+24
-30
next-proxy
bin/next-proxy
+1
-1
rules.json
examples/with-zones/rules.json
+6
-10
proxy.js
server/proxy.js
+17
-19
No files found.
bin/next-proxy
View file @
18d31f60
...
@@ -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
)
{
...
...
examples/with-zones/rules.json
View file @
18d31f60
[
{
{
"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
server/proxy.js
View file @
18d31f60
...
@@ -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'
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment