-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (75 loc) · 2.5 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
const express = require("express");
const helmet = require("helmet");
const cors = require("cors");
const logger = require("morgan");
const server = express();
const nodemailer = require("nodemailer");
require("dotenv").config();
const whitelist = [
"http://localhost:3000",
"http://localhost:3001",
"https://fitness-time.vercel.app/",
"https://www.fitness-time.app/",
"https://www.fitness-time.app",
"https://fitness-time.app/",
"https://fitness-time.app",
"http://localhost:3000/auth/adminlogin",
"https://fitness-time.vercel.app/auth/adminlogin",
"https://www.fitness-time.app/auth/adminlogin",
];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(console.log("Not allowed by CORS", origin));
}
},
};
const webmoSender = '"Sascha Majewsky" <s.majewsky93@gmail.com>';
const webmoTransporter = nodemailer.createTransport({
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
secure: true,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
});
server.use(cors(corsOptions));
server.use(helmet());
server.use(express.json());
server.use(logger("dev"));
server.disable("etag");
server.get("/", function rootHandler(req, res) {
res.status(200).json({
message: `Welcome to the ${process.env.DEPLOYMENT} environment API of fitness time!`,
});
});
server.post("/email", function rootHandler(req, res) {
const { name, email, message } = req.body;
if (email && name && message) {
webmoTransporter.sendMail({
from: webmoSender,
to: email,
bcc: webmoSender,
subject: "Fitness Time Email",
text: "We have received your message " + message,
html: "<h3>We have received your message</h3><br>" + message,
});
res.status(200).json({
message: `EMAIL SENT!`,
});
} else {
res.status(400).json({
message: `Please fill out all fields!`,
});
}
});
const UsersRouter = require("./api/resources/users/users-router");
const WorkoutsRouter = require("./api/resources/workouts/workouts-router");
const WeeklyWorkoutsPlansRouter = require("./api/resources/weekly-workout-plans/weekly-workout-plans-router");
server.use("/v1/users", UsersRouter);
server.use("/v1/workouts", WorkoutsRouter);
server.use("/v1/weekly-workout-plans", WeeklyWorkoutsPlansRouter);
module.exports = server;