-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (51 loc) · 1.73 KB
/
server.js
File metadata and controls
60 lines (51 loc) · 1.73 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
60
require('./mock/.runtime.js');
const Koa = require('koa');
const axios = require('axios');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
const { port, tpl, url2mock, statics } = require('./config');
const request = async (url) => {
const now = Date.now();
let result = '';
let headers = '';
try {
const res = await axios.get('https://jsonplaceholder.typicode.com' + url, {
proxy: false,
});
result = JSON.stringify(res.data, null, 2);
headers = JSON.stringify(res.headers, null, 2);
} catch(err) {
console.log('axios.get err:', url, '\n', err.stack)
result = err.message;
headers = '-';
}
const spent = Date.now() - now;
return [spent, headers, result];
};
// Set up your controllers
router.get('/', ctx => ctx.redirect('/dynamic'));
router.get('/default.min.css', ctx => {
ctx.set('Content-Type', 'text/css; charset=utf-8');
ctx.body = statics['/default.min.css'];
});
router.get('/highlight.min.js', ctx => {
ctx.set('Content-Type', 'text/javascript; charset=utf-8');
ctx.body = statics['/highlight.min.js'];
});
for(const [url, mockData] of Object.entries(url2mock)) {
router.get(url, async ctx => {
const [spent, headers, result] = await request(ctx.request.url);
render(ctx, { mockData, result, spent, headers });
});
}
app.use(router.routes()).use(router.allowedMethods());
app.listen(port);
console.log(`Listion at: http://localhost:${port}`);
function render(ctx, data = {}) {
ctx.body = tpl
.replace('__mock_definitions__', data.mockData || '')
.replace('__request_result__', data.result || '')
.replace('Spent: -', `Spent: ${data.spent || ''}`)
.replace('Response Headers: -', `Response Headers: ${data.headers || ''}`);
}