-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
58 lines (48 loc) · 1.55 KB
/
index.ts
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
// Initialize & Configure Server
const server = require("fastify")({logger: true});
// Require modules
const path = require('node:path');
require('dotenv').config()
// Register fastify module
server.register(require('@fastify/formbody'))
server.register(require('@fastify/helmet'), {
contentSecurityPolicy: false
});
server.register(require('@fastify/static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/', // optional: default '/'
});
// Initialize & Configure MongoDB
const mongoose = require('mongoose');
mongoose.connect(process.env.DATABASE_URL).then(
console.log('Database connected')
).catch((err: any) => console.log(err));
// Import index
// const index = fs.createReadStream(path('./public/index.html'))
// Import Controllers
const entityRoutes = require("./routes/entityRoutes");
// Configure Routes
server.get("/", function (req: any, res: { raw: { setHeader: (arg0: string, arg1: string) => void; }; sendFile: (arg0: string) => void; }) {
res.raw.setHeader("Content-Type", "text/html");
res.sendFile("index.html");
});
// Initiate routes from controller(s)
server.register(entityRoutes, { prefix: "/api/entity"});
// Listening
const start = async () => {
try {
await server.listen({port: process.env.PORT}, (err: any, address: any) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening at ${address}`)
});
console.log("Server running");
console.log(server.printRoutes());
} catch (err) {
server.log.error(err);
process.exit(1);
}
}
start();