-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusing-https.js
57 lines (49 loc) · 1.71 KB
/
using-https.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
50
51
52
53
54
55
56
/**
* (c) 2017 Radio France
* This program is free software: you can redistribute it and/or modify it under the terms of the CeCILL-B license
*/
/*
* HTTPS-to-HTTPS Example
*/
/* eslint-disable no-console */
const https = require('https')
const Forwarder = require('../lib/Forwarder')
const fs = require('fs')
const path = require('path')
const key = fs.readFileSync(path.join(__dirname, '..', 'test', 'ssl', 'key.pem'))
const cert = fs.readFileSync(path.join(__dirname, '..', 'test', 'ssl', 'cert.pem'))
const server = new Forwarder({
https: true,
// Options passed to the https.createServer
// cf. https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
httpsOpts: { key, cert },
// The servers to forward the request to (also using HTTPS in this example)
targets: ['https://127.0.0.1:9001', 'https://127.0.0.1:9002'],
// Options passed to the https.request call
// cf. https://nodejs.org/api/https.html#https_https_request_options_callback
targetOpts: {
rejectUnauthorized: false
}
})
server.listen(9000)
// Start HTTPS target servers
https.createServer({ key, cert }, (req, res) => {
console.log(`TARGET https://127.0.0.1:9001 Received: ${req.method} ${req.url}.`)
res.end()
}).listen(9001)
https.createServer({ key, cert }, (req, res) => {
console.log(`TARGET https://127.0.0.1:9902 Received: ${req.method} ${req.url}.`)
res.end()
}).listen(9002)
// Send a request
console.log('SCRIPT: Sending a request to the forwarder: POST /somepath')
https.request(
{
hostname: '127.0.0.1',
port: 9000,
path: '/somepath',
method: 'POST',
rejectUnauthorized: false
},
res => console.log(`SCRIPT: forwarder Replied ${res.statusCode}:${res.statusMessage}`)
).end()