Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
641 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const http = require('http')
|
---|
| 4 | const https = require('https')
|
---|
| 5 | const server = http.createServer(handler)
|
---|
| 6 | const port = +process.argv[2]
|
---|
| 7 | const prefix = process.argv[3]
|
---|
| 8 | const upstream = process.argv[4]
|
---|
| 9 | var calls = 0
|
---|
| 10 |
|
---|
| 11 | server.listen(port)
|
---|
| 12 |
|
---|
| 13 | function 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 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.