-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.js
49 lines (43 loc) · 1.37 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import http from 'http'
import { handleRequest } from './reverse-proxy.js'
import { proxyURL } from './url.js'
const server = http.createServer(async (req, res) => {
console.log(`Incoming request: ${req.method} ${req.url}`)
if (req.url.startsWith("/src:")) {
// for some reason, was getting some infinite loop, this stops it...
return
}
try {
// console.log(req)
// console.log("request.url:", req.url)
// console.log(req.headers)
let rurl = req.url
if (!rurl.startsWith("https://")) {
// need to add a host or new Request will fail
rurl = "https://localhost:8080" + req.url
}
console.log(rurl)
// request.url = rurl
let ropts = {
method: req.method,
headers: req.headers,
duplex: "half",
}
if (req.method == "POST") {
ropts.body = req
}
let preq = new Request(rurl, ropts)
let r = await handleRequest(preq)
console.log("R:", r.status, r)
res.writeHead(r.status, { 'Content-Type': r.headers.get('content-type') })
res.end(await r.text())
} catch (e) {
console.error(e)
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: { message: 'Internal Server Error' } }))
}
})
const PORT = process.env.PORT || 80 // Port for the reverse proxy
server.listen(PORT, () => {
console.log(`Reverse proxy listening on port ${PORT}`)
})