-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.06 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
const {validate} = require('./validator');
const fastify = require('fastify')({
logger: true,
});
const validationRequestSchema = {
schema: {
body: {
type: 'object',
required: ['schema', 'data'],
properties: {
'schema': {
type: 'object',
},
'data': {
type: 'object',
},
},
},
},
};
fastify.post('/', validationRequestSchema, async (request, response) => {
try {
const {schema, data} = request.body;
if (!schema || !data) {
return response.status(400).send('Missing schema and/or form data');
}
const submission = await validate(schema, data);
return response.send(submission);
} catch (error) {
if (error.name === 'ValidationError') {
return response.status(409).send(error);
}
request.log.error(error);
return response.status(500).send(error);
}
});
fastify.listen({port: 3000}, function(err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Server is now listening on ${address}`);
});