-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (88 loc) · 2.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-disable no-console */
const http = require('http');
const Xeno = require('..');
const app = new Xeno({
// querystringParser: (queryStr) => {},
errorHandler: async (err, ctx) => {
ctx.res.status(err.status || 500);
ctx.res.header('x-error-info', 'something');
ctx.res.body = err.message;
},
});
app.addHook('onRequest', async (ctx) => {
console.log('onRequest: received request and created ctx');
console.log(ctx.req.method, ctx.req.url, ctx.req.headers);
});
app.addHook('onParse', async (ctx) => {
console.log('onParse: request parsed');
console.log(ctx.req.query);
console.log(ctx.req.rawBody);
console.log(ctx.req.body);
});
app.addHook('onRoute', async (ctx) => {
console.log('onRoute: route and path params identified');
console.log('path params', ctx.req.params);
console.log('found route', ctx.req.route);
});
app.addHook('onSend', async (ctx) => {
console.log('onSend: response to be sent');
console.log(ctx.res.status());
console.log(ctx.res.headers());
console.log(ctx.res.body);
});
app.addHook('onResponse', (ctx) => {
console.log('onResponse: response sent');
console.log(ctx.res.status());
console.log(ctx.res.headers());
console.log(ctx.res.body);
});
app.addRoute({
url: '/minimal',
async handler() {
// do nothing
},
});
app.addRoute({
url: '/',
async handler(ctx) {
ctx.res.status(418);
ctx.res.header('powered-by', 'xeno');
ctx.res.headers({
'x-b3-traceid': '1234',
'powered-by': 'xeno', // overrides earlier one
});
ctx.res.body = ctx.req;
},
config: { hello: 'world' }, // available in ctx.req.route
});
app.addRoute({
method: 'post',
url: '/accounts',
async handler(ctx) {
ctx.res.status(201);
ctx.res.body = ctx.req.body;
},
});
app.addRoute({
url: '/accounts/:acctId',
async handler(ctx) {
ctx.res.body = ctx.req.params;
},
});
app.addRoute({
url: '/accounts/:acctId/txns/:txns',
async handler(ctx) {
ctx.res.body = ctx.req.params;
},
});
app.addRoute({
method: ['get', 'post'],
url: '/proxy/*all',
async handler(ctx) {
ctx.res.body = `I am a proxy for ${ctx.req.method} ${ctx.req.url}`;
},
});
app.start(http, {}, 3000, (...args) => {
console.log('server started');
console.log(...args);
});