-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (35 loc) · 1.25 KB
/
server.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
// const express = require('express');
// const app = express();
// const { hostname } = require('os');
//
// const STACK_NAME = process.env.STACK_NAME || "Unknown Stack";
// const port = 8080;
// const message = `Hello from host: ${hostname()}:${port} in ${STACK_NAME}\n`
//
// app.get('/', (req, res) => {
// res.send(message);
// });
//
// app.listen(port, () => console.log(`Server running at http://${hostname()}:${port}/`));
const { hostname } = require('os');
const https = require('https');
const fs = require('fs');
const STACK_NAME = process.env.STACK_NAME || "Unknown Stack";
const httpsPort = 8443;
const httpsKey = '../keys/key.pem'
const httpsCert = '../keys/cert.pem'
if (fs.existsSync(httpsKey) && fs.existsSync(httpsCert)) {
console.log('Starting https server')
const message = `Hello HTTPS World from ${hostname()} in ${STACK_NAME}\n`;
const options = { key: fs.readFileSync(httpsKey), cert: fs.readFileSync(httpsCert) };
const server = https.createServer(options, (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(message);
});
server.listen(httpsPort, hostname, () => {
console.log(`Server running at http://${hostname()}:${httpsPort}/`);
});
} else {
console.log('Could not find certificate/key');
}