forked from goitacademy/nodejs-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (91 loc) · 1.98 KB
/
server.js
File metadata and controls
101 lines (91 loc) · 1.98 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
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
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const nodemailer = require("nodemailer");
require("dotenv").config();
const app = express();
const path = require("path");
app.use(
express.static(
path.join(__dirname, "public")
)
);
app.use(express.json());
app.use(cors());
const contactsRouter = require("./routes/api");
app.use("/api", contactsRouter);
app.use((_, res, __) => {
res.status(404).json({
status: "error",
code: 404,
message:
"Use api on routes: /api/index",
data: "Not found",
});
});
const port = process.env.PORT || 3000;
const dbHost = process.env.DB_HOST;
mongoose.set("strictQuery", true);
const connection = mongoose.connect(
dbHost,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
connection
.then(() => {
app.listen(port, () => {
console.log(
`Database connection successful. Server running. Use our API on port: ${port}`
);
});
})
.catch((err) => {
console.log(
`Server not running. Error message: ${err.message}`
);
process.exit(1);
});
app.post(
"/send-email",
(req, res, next) => {
const { email, name, text } =
req.body;
const config = {
host: "smtp.meta.ua",
port: 465,
secure: true,
auth: {
user: "konievanatol@meta.ua",
pass: process.env.PASSWORD,
},
};
const transporter =
nodemailer.createTransport(
config
);
const emailOptions = {
from: "konievanatol@meta.ua",
to: email,
subject: "Nodemailer test",
text: `${text}`,
};
transporter
.sendMail(emailOptions)
.then((info) =>
res.json({
message: "Email sent",
})
)
.catch((err) => next(err));
}
);
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
status: "fail",
code: err.status || 500,
message: err.message,
data: "Internal Server Error",
});
});