/
server.js
70 lines (61 loc) · 1.59 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Import Model
const Post = require("./models/Post");
// Connect to MongoDB
mongoose.connect(
"mongodb://localhost:27017/simple-mern",
() => console.log("MongoDB is connected")
);
// Enable CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// Get all of our posts
app.get("/api/posts", (req, res) => {
Post.find({}).then(posts => {
res.json(posts);
});
});
// Get One of Our posts
app.get("/api/posts/:id", (req, res) => {
Post.findOne({ _id: req.params.id }).then(post => {
res.json(post);
});
});
// Create and Update post
app.post("/api/posts", (req, res) => {
const data = {
title: req.body.title,
content: req.body.content
};
Post.findOne({ _id: req.body.id }, (err, post) => {
if (post) {
Post.findByIdAndUpdate(req.body.id, data, { upsert: false }).then(
updated => {
res.json(updated);
}
);
} else {
Post.create(data).then(created => {
res.json(created);
});
}
});
});
// Delete selected post
app.post("/api/posts/:id", (req, res) => {
Post.findByIdAndDelete(req.params.id).then(post => {
res.json({ message: "Your post was deleted!" });
});
});
app.listen(3333, () => console.log("Server is running on port 3333"));