-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (113 loc) · 2.04 KB
/
app.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require('express');
const app = express();
const JSONAPISerializer = require('jsonapi-serializer').Serializer;
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); // update to match the domain you will make the request from
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
const agreements = [
{
id: '0',
content: 'bonjour',
users: [
{
id: '0',
name: 'Adrien'
},
{
id: '1',
name: 'Adrien2'
}
]
},
{
id: '1',
content: 'salut',
users: [
{
id: '2',
name: 'Adrien3'
}
]
},
{
id: '2',
content: 'yo',
users: [
{
id: '0',
name: 'Adrien'
}
]
}
];
const users = [
{
id: '0',
name: 'Adrien',
agreements: [
{
id: '0',
content: 'bonjour'
},
{
id: '2',
content: 'yo'
}
]
},
{
id: '1',
name: 'Adrien2',
agreements: [
{
id: '0',
content: 'bonjour'
}
]
},
{
id: '2',
name: 'Adrien3',
agreements: [
{
id: '1',
content: 'salut'
}
]
}
];
var agreementSerializer = new JSONAPISerializer('agreement', {
attributes: ['content', 'users'],
users: {
ref: 'id',
attributes: ['name']
}
});
var userSerializer = new JSONAPISerializer('user', {
attributes: ['name', 'agreements'],
agreements: {
ref: 'id',
included: false,
attributes: ['content']
}
});
app.get('/agreements', (req, res) => {
res.send(agreementSerializer.serialize(agreements));
});
app.get('/agreements/:id', (req, res) => {
res.send(
agreementSerializer.serialize(agreements[parseInt(req.params['id'])])
);
});
app.get('/users', (req, res) => {
res.send(userSerializer.serialize(users));
});
app.get('/users/:id', (req, res) => {
res.send(userSerializer.serialize(users[parseInt(req.params['id'])]));
});
app.listen(3000);