-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (126 loc) · 3.62 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Lib imports
import express from "express";
import * as http from "http";
import dotenv from "dotenv";
import bodyParser from "body-parser";
import cors from "cors";
import helmet from "helmet";
import morgan from "morgan";
import mongoose from "mongoose";
import { MailTransporter } from "./lib/mailer.js";
import User from "./models/user.js";
import nodemailer from "nodemailer";
import checkUser from "./middlewares/checkUser.js";
import * as jwt from "jsonwebtoken";
import fs from "fs";
import path from "path";
import cookieParser from "cookie-parser";
import jsonwebtoken from "jsonwebtoken";
import { validate } from "email-validator";
import bcrypt from "bcryptjs";
import * as url from "url";
import recognize from "./lib/TesseractDetect.js";
// await recognize("file.png").then((res) => console.log(res));
// App init
const app = express();
dotenv.config();
const build = false;
app.use(cookieParser());
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
console.log(__dirname);
console.log(path.join(__dirname, "client/build"));
// middleware setup
app.use(express.json());
app.use(cors({ credentials: true, origin: true }));
const server = http.Server(app);
// DB connections
mongoose
.connect(process.env.HEALTHCARE_DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to database successfully");
})
.catch((err) => {
console.log(err);
});
// Route setup
import userRouter from "./routes/userRouter.js";
app.use("/api/users/", userRouter);
import reminderRouter from "./routes/reminderRouter.js";
app.use("/api/reminders/", reminderRouter);
import fileRouter from "./routes/fileRouter.js";
app.use("/api/files/", fileRouter);
import contactRouter from "./routes/contactRouter.js";
app.use("/api/contacts/", contactRouter);
// ejs view engine
app.set("view engine", "ejs");
// Root route
const mailTransporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
});
app.get("/api/sendMail", checkUser, async (req, res) => {
if (!req.query?.subject)
return res.status(400).json("No subject or body for mail.");
const user = await User.findById(req.checkData.id);
if (!user) return res.status(403).json("User not found");
console.log(user);
const subject = req.query.subject;
let body;
if (!req.query.body)
body = `${req.checkData.username}'s Corto Companion is attached! Thank you for using CortX.`;
else {
body = req.query.body;
}
let recepient = user.email;
if (req.query.recepient) recepient = req.query.recepient;
const file = fs.readFileSync(path.join(__dirname, "./data.csv"));
mailTransporter.sendMail(
{
from: "<cortxapp@gmail.com> CortX App",
to: recepient,
subject: subject,
text: body,
attachments: [
{
filename: "Your Data.csv",
content: file,
},
],
},
(err, info) => {
if (err) {
console.log(err);
return res
.status(400)
.json({ status: "error", error: "Could not send." });
} else {
return res.json({ status: "success" });
}
}
);
});
if (
process.env.NODE_ENV === "production" ||
process.env.NODE_ENV === "staging" ||
build
) {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build", "index.html"));
});
}
const listener = server.listen(process.env.PORT || 5000, (err) => {
if (err) {
console.log(err);
} else {
console.log(`Listening on ${listener.address().port}`);
}
});