-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.7 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
import { join } from 'path'
import AutoLoad from '@fastify/autoload'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs';
import { GUIDES_FOLDER } from './const.js';
import fastifyStatic from '@fastify/static';
import cors from '@fastify/cors'
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export default async function (fastify, opts) {
// Place here your custom code!
await fastify.register(cors, {
// put your options here
})
if (!fs.existsSync(GUIDES_FOLDER)) {
fs.mkdirSync(GUIDES_FOLDER, { recursive: true });
};
// 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: 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: join(__dirname, 'routes'),
options: Object.assign({}, opts)
});
fastify.register(fastifyStatic, {
root: join(__dirname, GUIDES_FOLDER),
prefix: '/guides/',
});
fastify.decorate('notFound', (request, reply) => {
reply.code(404).type('text/html').send('Not Found')
});
fastify.setNotFoundHandler(fastify.notFound);
fastify.setErrorHandler(function (error, request, reply) {
if (error instanceof Fastify.errorCodes.FST_ERR_BAD_STATUS_CODE) {
// Log error
this.log.error(error)
// Send error response
reply.status(500).send({ ok: false })
} else {
// fastify will use parent error handler to handle this
reply.send(error)
}
})
}