UNPKG

641 BJavaScriptView Raw
1'use strict'
2
3const http = require('http')
4const https = require('https')
5const server = http.createServer(handler)
6const port = +process.argv[2]
7const prefix = process.argv[3]
8const upstream = process.argv[4]
9var calls = 0
10
11server.listen(port)
12
13function handler (req, res) {
14 if (req.url.indexOf(prefix) !== 0) {
15 throw new Error('request url [' + req.url + '] does not start with [' + prefix + ']')
16 }
17
18 var upstreamUrl = upstream + req.url.substring(prefix.length)
19 https.get(upstreamUrl, function (ures) {
20 ures.on('end', function () {
21 if (++calls === 2) {
22 server.close()
23 }
24 })
25 ures.pipe(res)
26 })
27}