Skip to content

Commit 1d4172d

Browse files
committed
feat: add HTTP gateway demo with request and response hooks
1 parent 83d08d3 commit 1d4172d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

demos/hooks-ts-example.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
import { IncomingMessage } from 'http'
4+
import gateway from '../index'
5+
import { http } from '../lib/default-hooks'
6+
import { ServerResponse } from 'http'
7+
8+
gateway({
9+
routes: [
10+
{
11+
prefix: '/httpbin',
12+
target: 'https://httpbin.org',
13+
hooks: {
14+
onRequest: (req: IncomingMessage, res: ServerResponse) => {
15+
console.log('Request to httpbin.org')
16+
17+
return false
18+
},
19+
onResponse: (
20+
req: IncomingMessage,
21+
res: ServerResponse,
22+
proxyRequest: IncomingMessage,
23+
) => {
24+
console.log('POST request to httpbin.org')
25+
26+
// continue forwarding the response
27+
http.onResponse(req, res, proxyRequest)
28+
},
29+
},
30+
},
31+
],
32+
}).start(8080)

0 commit comments

Comments
 (0)