-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
76 lines (74 loc) · 1.76 KB
/
server.ts
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
import { run, app, act, cond, and, or } from '@barajs/core'
import Express, {
whenExpressStarted,
whenAnyGet,
whenRootGet,
whenAnyPost,
whenCustomGet,
hasGetQuery,
hasMountPoint,
WhenRequest,
hasPostPath,
WhenCustomRoute,
hasGetPath,
sendJSONResultOf,
} from './src'
run(
app({
portion: [
Express({
port: 3200,
routes: {
get: ['/item/:id'],
},
}),
],
trigger: [
whenExpressStarted(
act(({ port }: any) =>
console.log(`Express server started on http://localhost:${port}`),
),
act(() => console.log('This is the demo Expres server with Bara')),
),
whenRootGet(
act(
sendJSONResultOf(() =>
Promise.resolve({ success: true, hello: 'world' }),
),
),
),
whenAnyGet(
cond(
and(
hasGetPath('first'),
or(hasGetQuery('pretty'), hasGetQuery('clean')),
),
act(({ request, response }: WhenRequest) => {
const { query } = request
response.send({ success: true, query })
}),
),
act(({ request }: WhenRequest) => {
console.log(`[/GET] ${request.originalUrl}`)
}),
),
whenCustomGet(
cond(
hasMountPoint('/item/:id'),
act(({ response, mountPoint }: WhenCustomRoute) => {
response.send({ success: true, mountPoint })
}),
),
),
whenAnyPost(
cond(
hasPostPath('/webhook'),
act(({ request, response }: WhenRequest) => {
const { query, originalUrl } = request
response.send({ success: true, query, originalUrl })
}),
),
),
],
}),
)