-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (58 loc) · 1.78 KB
/
index.js
File metadata and controls
75 lines (58 loc) · 1.78 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
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
//#region imports
import express from "express";
import cors from "cors";
// import mongoose from "mongoose";
import APIRoutes from "./src/Routes/index.js";
import bodyParser from "body-parser";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { PORT } from "./ENV.js";
import "./src/Helpers/MongoDB/index.js";
import path from "path";
import { updateLogo } from "./src/Controllers/CompaniesControllers/Get.js";
import { log } from "console";
//#endregion
//#region constants
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const indexPath = join(__dirname, "build", "index.html");
//#endregion
//#region middlewares
const app = express();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
// app.use((req, res, next) => {
// console.log(req.path, req.method, req.body);
// next();
// });
// Serve static files
app.use(express.static(path.join(__dirname, "build")));
// Define API routes
app.use("/api", APIRoutes);
// Error handling middleware for 404 errors
app.use("/api/*", (req, res, next) => {
const err = new Error("Api Not Found");
err.status = 404;
next(err);
});
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
// Error handling middleware for other errors
app.use(function (err, req, res, next) {
// Set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// Render the error page
res.status(err.status || 500);
res.json({
error: err.message,
});
});
//#endregion
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// const result = await updateLogo();
// console.log(result);