forked from julianduque/fastify-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (45 loc) · 1.06 KB
/
server.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
'use strict'
const path = require('path')
const fastify = require('fastify')({
logger: {
prettyPrint: true
}
})
const port = process.env.PORT || 3000
fastify.register(require('point-of-view'), {
engine: {
handlebars: require('handlebars')
}
})
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/'
})
const queryschema = {
schema: {
querystring: {
type: 'object',
properties: {
name: {
type: 'string'
},
course: {
type: 'string'
}
},
required: ['course']
}
}
}
fastify.get('/', queryschema, (request, reply) => {
const name = request.query.name || 'Anonymous'
const course = request.query.course || 'course'
reply.view('/templates/index.hbs', { name, course })
})
async function start() {
await fastify.listen(port)
}
start().catch(err => {
fastify.log.error(err)
process.exit(1)
})