/
express.ts
61 lines (53 loc) · 1.67 KB
/
express.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Express.js example web server twith tracker.
*
*
* To run this you need to set environment variable TOP_WEB_API_KEY
* to a random string.
*
* export TOP_WEB_API_KEY=`node -e "console.log(crypto.randomBytes(20).toString('hex'));"`
* echo "API key is $TOP_WEB_API_KEY"
*
* npm run build # Compile TS
* node build/src/express.js
*
*/
import express from 'express';
import { Tracker, TrackerServer, createTrackerMiddleware } from './index';
// Create Polka server
// See https://github.com/lukeed/polka
// for more information.
// (Polka is the default for SvelteKit)
const app = express();
// Create HTTP request tracker.
// This will store active and completed HTTP requests
// in Node.js process memory.
const tracker = new Tracker();
// Install HTTP request start/end hooks.
// If no max completed request buffer size is not given,
// read the max number from TOP_MAX_COMPLETED_TASKS
// environment variable, or default to 256 requests
// if not set.
const trackerMiddleware = createTrackerMiddleware(tracker);
app.use(trackerMiddleware);
// Install API endpoint.
// If no API key is given read one from
// TOP_WEB_API_KEY environment variable.
const trackerServer = new TrackerServer(tracker);
// Under which path we install the tracker API endpoint
const trackerPath = '/tracker';
app.get(trackerPath, trackerServer.serve.bind(trackerServer));
// Create Hello world landing page
app.get('/', async (req, res) => {
res.end('Hello world');
});
// Start web server
app.listen(3000, () => {
console.log('Listening on port 3000');
console.log(
`HTTP active requests API at ${trackerPath}, API key starts as ${trackerServer.apiKey.slice(
0,
4
)}…`
);
});