Skip to content

Commit 66e1293

Browse files
committed
added https example
1 parent 5943efa commit 66e1293

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

examples/https/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# HTTPS example
2+
3+
Simple HTTPS server using `tinyhttp` and `https` module.
4+
5+
## Setup
6+
Generate the certificate key.
7+
8+
```sh
9+
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
10+
-keyout localhost-privkey.pem -out localhost-cert.pem
11+
```
12+
13+
Install all dependencies
14+
```sh
15+
pnpm install
16+
```
17+
18+
## Run
19+
```sh
20+
node index.js
21+
```
22+
23+
and in another terminal:
24+
25+
```sh
26+
curl https://localhost:3000 -k
27+
```
28+
29+
expected output:
30+
31+
```sh
32+
Hello from https server!
33+
```

examples/https/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { App } from '@tinyhttp/app'
2+
import fs from 'fs'
3+
import https from 'https'
4+
5+
const app = new App({
6+
settings: {
7+
networkExtensions: true,
8+
},
9+
})
10+
11+
const options = {
12+
key: fs.readFileSync('localhost-privkey.pem'),
13+
cert: fs.readFileSync('localhost-cert.pem'),
14+
}
15+
16+
app.get('/', (req, res) => res.send(`Hello from ${req.protocol} server!`))
17+
18+
const server = https.createServer(options)
19+
20+
server.on('request', async (req, res) => await app.handler(req, res)).listen(3000)

examples/https/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "https",
3+
"type": "module",
4+
"dependencies": {
5+
"@tinyhttp/app": "^0.2.29"
6+
}
7+
}

0 commit comments

Comments
 (0)