-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (41 loc) · 1.24 KB
/
app.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
const express = require("express");
const mongoose = require("mongoose");
const app = express();
app.use(express.json());
const port = 5000;
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
});
const Comment = require("./models/Comment");
const DBUrl = `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`;
mongoose
.connect(DBUrl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Connected to DB");
})
.catch(() => {
console.log("Failed DB");
});
app.post("/api/createComment", (req, res) => {
const nick = req.body.name;
const comment = req.body.comment;
if (
nick.length > 28 ||
comment.length > 100 ||
nick.length < 1 ||
comment.length < 1
) {
res
.status(400)
.json({ message: "Nick lub komentarz nie spełnia wymagań." });
} else {
const newComment = new Comment({ nick, comment, accepted: false });
newComment
.save()
.then(() => res.json({ message: "success" }))
.catch(() =>
res.status(400).json({ message: "Błąd dodawania komentarza" })
);
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));