-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (38 loc) · 1.08 KB
/
server.js
File metadata and controls
47 lines (38 loc) · 1.08 KB
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
import Fastify from "fastify";
import cors from "@fastify/cors";
import serviceRoutes from "./src/server/routes.js";
const fastify = Fastify({
logger: true,
});
import fastifyStatic from "@fastify/static";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Register CORS
fastify.register(cors, {
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE"],
});
// Register Static files (for serving built frontend)
fastify.register(fastifyStatic, {
root: path.join(__dirname, "dist"),
prefix: "/",
});
// Register Routes
fastify.register(serviceRoutes);
// Catch-all route to serve index.html for SPA routing
fastify.setNotFoundHandler((request, reply) => {
reply.sendFile("index.html");
});
const start = async () => {
try {
const PORT = process.env.PORT || 1337;
await fastify.listen({ port: PORT, host: "0.0.0.0" });
console.log(`Server is running on http://localhost:${PORT}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();