-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (42 loc) · 1.46 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require('express');
const app = express('/');
const ServerTiming = require('servertiming');
const timing = new ServerTiming();
const mockData = require('./resources/mockData_1000.json');
/**
* Sample routes that handle server side user
* timing
*/
app.get('/timing', (req, res) => {
setTimeout(()=> {
// // All timings should be in milliseconds (s)
timing.addMetric("Locking Tractor Beam", 50.0);
timing.addMetric("Cheesy Quote", 150.0);
timing.addMetric("Applying Lasers", 75.0);
// JSON with 100 random images from our data
const rdmImages = (new Array(100)).fill(0)
.map(idx => Math.floor(Math.random()*mockData.length-1))
.map(idx => mockData[idx])
.reduce((acc, img) => [...acc, {img: img.imageUrl}], [])
res.setHeader("Server-Timing", timing.generateHeader());
res.status(200).json(rdmImages);
}, 500);
});
app.get('/data/:seed', (req, res) => {
// simulate a slight delay
setTimeout(()=> {
res.status(200).json(mockData);
}, 500);
})
const resp = {hello:'world'};
app.get('/echo', (req, res)=> res.status(200).json(resp));
app.get('/echo/:time', (req, res) => {
const time = parseInt(req.params.time, 10) || 0;
setTimeout(()=> {
res.status(200).json(resp);
}, time)
})
app.use(express.static('public'));
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})