-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (78 loc) · 2.28 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
require("dotenv").config();
const path = require("path");
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const { resolveSoa } = require("dns");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
const port = process.env.PORT || 3001;
//DB configs
mongoose
.connect(process.env.MONGO_URI).then(() => console.log("connected to MongoDB successfully"))
.catch((err) => console.log(err));
const postSchema = mongoose.Schema({
title: String,
content: String,
fromDate: Date,
toDate: Date,
instructor: String,
});
const Post = mongoose.model("Post", postSchema);
app.post("/create", (req, res) => {
const newPost = new Post({
title: req.body.title,
content: req.body.content,
fromDate: req.body.fromDate,
toDate: req.body.toDate,
instructor: req.body.instructor,
});
newPost
.save()
.then((doc) => console.log(doc))
.catch((err) => console.log(err));
});
app.post("/search", async (req, res) => {
let payload = req.body.payload.trim();
console.log(payload);
let search = await Post.find({ title: { $regex: new RegExp('^' + payload + '.*', 'i') } }).exec();
//limit search
search = search.slice(0, 10);
res.send({ payload: search });
});
app.get("/posts", (req, res) => {
Post.find()
.then((items) => res.json(items))
.catch((err) => console.log(err));
});
app.delete("/delete/:id", (req, res) => {
console.log(req.params);
Post.findByIdAndDelete({ _id: req.params.id })
.then((doc) => console.log(doc))
.catch((err) => console.log(err));
});
app.put("/update/:id", (req, res) => {
Post.findByIdAndUpdate(
{ _id: req.params.id },
{
title: req.body.title,
content: req.body.content,
fromDate: req.body.fromDate,
toDate: req.body.toDate,
instructor: req.body.instructor,
}
)
.then((doc) => console.log(doc))
.catch((err) => console.log(err));
});
if (process.env.NODE_ENV === 'production') {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.listen(process.env.PORT || 3001, function () {
console.log(`workshop's server is running at port ${port}`);
});