-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (48 loc) · 1.52 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
"use strict";
// 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) => 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, res) {
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, address) => {
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();