-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (77 loc) · 2.53 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
'use strict'
const path = require('path')
const AutoLoad = require('fastify-autoload')
const fastifyStatic = require('fastify-static')
const fastify = require('fastify')({
logger: true
})
// const start = async () => {
// try {
// await fastify.listen()
// } catch (err) {
// fastify.log.error(err)
// process.exit(1)
// }
// }
// start()
// Run the server!
// fastify.listen(3001, function (err, address) {
// if (err) {
// fastify.log.error(err)
// process.exit(1)
// }
// // Server is now listening on ${address}
// })
module.exports = async function (fastify, opts) {
// Place here your custom code!
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: Object.assign({}, opts)
})
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: Object.assign({}, opts)
})
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'public'),
prefix: '/public/', // optional: default '/'
index: false,
list: true
})
// fastify.register(fastifyStatic, {
// root: path.join(__dirname, 'wtf'),
// prefix: '/wtf/', // optional: default '/'
// decorateReply: false // the reply decorator has been added by the first plugin registration
// })
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'node_modules'),
prefix: '/js/',
decorateReply: false // the reply decorator has been added by the first plugin registration
})
// In this example, when you get localhost:3000, ou have the time
fastify.get('/time', (request, reply) => {
reply.header('Content-Type','application/json')
reply.send({hello: new Date()})
})
fastify.get('/about', (request, reply) => {
reply.sendFile('about.html')
})
fastify.get('/lewit', (request, reply) => {
reply.sendFile('lewit.js')
})
fastify.get('/', (request, reply) => {
reply.sendFile('foo.html')
})
// fastify.get('/another/path', function (req, reply) {
// return reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
// })
// fastify.get('/path/with/different/root', function (req, reply) {
// return reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
// })
}