finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
641 bytes
|
| Line | |
|---|
| 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.