-
Notifications
You must be signed in to change notification settings - Fork 94
/
example.js
55 lines (40 loc) · 1.13 KB
/
example.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
'use strict'
const Fastify = require('fastify')
const proxy = require('..')
async function startOrigin () {
const origin = Fastify()
origin.get('/', async (request, reply) => {
return 'this is root'
})
origin.get('/redirect', async (request, reply) => {
return reply.redirect(302, 'https://fastify.dev')
})
origin.get('/a', async (request, reply) => {
return 'this is a'
})
origin.post('/this-has-data', async (request, reply) => {
if (request.body.hello === 'world') {
return { something: 'posted' }
}
throw new Error('kaboom')
})
await origin.listen()
return origin
}
async function startProxy (upstream) {
const server = Fastify()
server.register(proxy, {
upstream,
prefix: '/upstream' // optional
})
await server.listen({ port: 3000 })
return server
}
async function run () {
const origin = await startOrigin()
const upstream = `http://localhost:${origin.server.address().port}`
console.log('origin started', upstream)
const proxy = await startProxy(upstream)
console.log('proxy started', `http://localhost:${proxy.server.address().port}/upstream/`)
}
run()