-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathhttps-server-example.ts
More file actions
59 lines (52 loc) · 1.76 KB
/
Copy pathhttps-server-example.ts
File metadata and controls
59 lines (52 loc) · 1.76 KB
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
57
58
59
/* eslint-disable no-console */
/**
* HTTPS Server Example
*
* This is an example demonstrating how to use the generated X509 certificate
* in a Node.js HTTPS server.
*
* Usage:
* 1. Generate a certificate using the X509 Certificate Generator tool
* 2. Save the certificate as 'certificate.crt' and private key as 'privateKey.key'
* 3. Create a 'cert' folder in your project root
* 4. Place the files in the cert folder
* 5. Run this file with: ts-node https-server-example.ts
* 6. Access the server at: https://localhost:8443
*
* Note: Since this is a self-signed certificate, your browser will show a security warning.
* You can proceed safely for development/testing purposes.
*/
import fs from 'node:fs';
import https from 'node:https';
import path from 'node:path';
const PORT = 8443;
// Certificate and private key file paths (modify as needed)
const crtPath = path.join(__dirname, 'cert/certificate.crt');
const keyPath = path.join(__dirname, 'cert/privateKey.key');
const options: https.ServerOptions = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(crtPath),
};
const server = https.createServer(options, (req, res) => {
const { method, url } = req;
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(
JSON.stringify(
{
code: 0,
message: 'HTTPS server is running',
method,
url,
},
null,
2,
),
);
});
server.listen(PORT, () => {
console.log(`HTTPS server: https://localhost:${PORT}`);
console.log(`Certificate: ${crtPath}`);
console.log(`Private Key: ${keyPath}`);
console.log('\nNote: Your browser will show a security warning because this is a self-signed certificate.');
console.log('This is normal for development/testing purposes.');
});