Skip to content

Commit 19f957a

Browse files
committed
forward request body & method
1 parent 33b7742 commit 19f957a

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch micro",
11+
"program": "${workspaceRoot}/node_modules/micro/bin/micro.js",
12+
"args": [
13+
"index.js"
14+
]
15+
}
16+
]
17+
}

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@ JavaScript, there is no way we could be impersonating Alice. Alice is safe.
3434
Bob was never protected in the first place. Eve has better things to do with
3535
her time.
3636

37+
## But I need to POST/PUT/etc with data?
38+
39+
That works too! Just make an OPTIONS/POST/PUT/DELETE/etc request and it will be forwarded.
40+
If you can only make GET requests, you can provide a `method` query parameter and
41+
the server will make it that kind of request instead.
42+
3743
## Supported headers
3844

3945
If there's a way to whitelist ALL headers, let me know. The one's I've explicitly added
4046
so far are:
4147

42-
- x-requested-with
48+
- accept-encoding
49+
- accept-language
50+
- accept
4351
- access-control-allow-origin
44-
- x-http-method-override
45-
- content-type
4652
- authorization
47-
- accept
48-
- connection
49-
- pragma
5053
- cache-control
54+
- connection
55+
- content-length
56+
- content-type
5157
- dnt
52-
- referer
53-
- accept-encoding
54-
- accept-language
58+
- pragma
5559
- range
60+
- referer
61+
- user-agent
62+
- x-http-method-override
63+
- x-requested-with
5664

5765
## That is nice, I want to run my own server
5866

index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ const url = require('url')
33
const pkg = require('./package.json')
44
const {send} = require('micro')
55
const allowHeaders = [
6-
'x-requested-with',
6+
'accept-encoding',
7+
'accept-language',
8+
'accept',
79
'access-control-allow-origin',
8-
'x-http-method-override',
9-
'content-type',
1010
'authorization',
11-
'accept',
12-
'connection',
13-
'pragma',
1411
'cache-control',
12+
'connection',
13+
'content-length',
14+
'content-type',
1515
'dnt',
16-
'referer',
17-
'accept-encoding',
18-
'accept-language',
16+
'pragma',
1917
'range',
18+
'referer',
19+
'user-agent',
20+
'x-http-method-override',
21+
'x-requested-with',
2022
]
2123
const cors = require('micro-cors')({allowHeaders})
2224
const fetch = require('node-fetch')
@@ -29,8 +31,20 @@ async function service (req, res) {
2931
<html>
3032
<title>400 Error</title>
3133
<h1>Missing 'href' parameter.</h1>
32-
<h1>Note: An optional 'method' parameter is also supported.</h1>
3334
<h2>See docs: <a href="https://npmjs.org/package/${pkg.name}">https://npmjs.org/package/${pkg.name}</a></h2>
35+
<h2>Supported HTTP Methods</h2>
36+
<ul>
37+
<li>All - OPTIONS, GET, POST, PUT, DELETE, etc</li>
38+
</ul>
39+
<h2>Supported Query Parameters</h2>
40+
<ul>
41+
<li>?href=&lt;href&gt; - <i>required</i>, the URL you are trying to reach</li>
42+
<li>&method=&lt;method&gt; - <i>optional</i>, the HTTP method to use</li>
43+
</ul>
44+
<h2>Supported Headers</h2>
45+
<ul>
46+
${allowHeaders.map(x => `<li>${x}</li>`).join('\n')}
47+
</ul>
3448
</html>
3549
`
3650
return send(res, 400, html)
@@ -42,8 +56,9 @@ async function service (req, res) {
4256
}
4357
}
4458
let f = await fetch(q.href, {
45-
method: q.method || 'GET',
46-
headers
59+
method: q.method || req.method,
60+
headers,
61+
body: req
4762
})
4863
f.body.pipe(res)
4964
}

0 commit comments

Comments
 (0)