-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpress-https.ts
45 lines (37 loc) · 1.08 KB
/
express-https.ts
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
/**
* This example shows how to use this library with Express using HTTPS
*/
import { createServer } from 'node:https';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import express from 'express';
import { FileSystemStorage } from '../src/send-stream';
const app = express();
app.disable('x-powered-by');
const storage = new FileSystemStorage(join(__dirname, 'assets'));
// eslint-disable-next-line @typescript-eslint/no-misused-promises
app.get('*', async (req, res, next) => {
try {
const result = await storage.prepareResponse(req.url, req);
if (result.statusCode === 404) {
next();
return;
}
await result.send(res);
} catch (err: unknown) {
// eslint-disable-next-line n/callback-return
next(err);
}
});
const server = createServer(
{
// eslint-disable-next-line n/no-sync
key: readFileSync(join(__dirname, 'cert', 'localhost.key')),
// eslint-disable-next-line n/no-sync
cert: readFileSync(join(__dirname, 'cert', 'localhost.crt')),
},
app,
);
server.listen(3001, () => {
console.info('listening on https://localhost:3001');
});