forked from sh3ngsh3ng/h4g-2024-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (52 loc) · 1.54 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cors from "cors";
import express from "express";
import fs from "fs";
import { authMiddleware } from "./middleware/auth-middleware";
import { generatorFromPdf, generatorFromScratch } from "./controllers/pdf";
const mongoose = require("mongoose");
require("dotenv").config();
require("./services/email");
require("./services/cloudinary");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const app = express();
// apply middleware
app.use(cors());
app.use(express.json());
app.use(morgan("dev"));
// db
mongoose
.connect(process.env.CLOUD_DATABASE, {})
.then(() => console.log("DB connected"))
.catch((err) => console.log("DB Error => ", err));
// route
fs.readdirSync("./routes").map((r) => app.use("/api", require(`./routes/${r}`)));
app.get("/api/test", authMiddleware, (req, res) => {
res.send("Hello world from node js");
});
app.get("/pdf1", generatorFromScratch);
app.get("/pdf2", generatorFromPdf);
app.get("/test-email", (req, res) => {
console.log("called");
res.send("ok");
});
app.get("/test-cloudinary", (req, res) => {
console.log("called");
res.send("ok");
});
// for deployment
const path = require("path");
const _dirname = path.dirname("");
const buildPath = path.join(_dirname, "/build");
app.use(express.static(buildPath));
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "/build/index.html"), function (err) {
if (err) {
res.status(500).send(err);
}
});
});
const port = 8000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});