This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
122 lines (99 loc) · 2.69 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
var express = require("express");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var Blog = require("./models/blog");
// Connect to the database with error check
mongoose.connect("mongodb://localhost/blog", function (err) {
if (err) {
console.log('connection error', err);
} else {
console.log('connection successful');
}
});
// Create the Express application
var app = express();
// Use environment defined port or 3000
var port = process.env.PORT || 3000;
// Use the body-parser package in our application
app.use(bodyParser.json());
// Register the router with the application
app.use(express.static(__dirname + "/public"));
// READ
// Create endpoint /api/blogs for POST
app.post("/api/blogs", function (req, res) {
// blogsRoute.post(function (req, res) {
// New instance of the Blog model
var blog = new Blog();
// Set the blog properties that came from the Post data
blog.title = req.body.title;
blog.content = req.body.content;
blog.author = req.body.author;
blog.date = req.body.date;
blog.comments = req.body.comments;
// Save the blog and check for errors
blog.save(function(err) {
if (err) {
res.send(err);
}
res.json(blog);
});
});
// Create endpoint for /api/blogs for GET
app.get("/api/blogs", function (req, res) {
Blog.find(function (err, blogs) {
if (err) {
res.send(err);
}
res.json(blogs);
});
});
// Create
// Create a new route for /blogs/:blog_id
app.get("/api/blogs/:blog_id", function (req, res) {
// Find a specific blog
Blog.findById(req.params.blog_id, function (err, blog) {
if (err) {
res.send(err);
}
res.json(blog);
});
});
// Update
// Change the blog
app.put("/api/blogs/:blogs_id", function (req, res) {
// Use the Blog model to find a specific blog
Blog.findById(req.params.blogs_id, function (err, blog) {
if (err) {
res.send(err);
}
blog.title = req.body.title;
blog.content = req.body.content;
blog.author = req.body.author;
blog.date = req.body.date;
blog.comments = req.body.comments;
// Save the blog and check for errors
blog.save(function (err) {
if (err) {
res.send(err);
}
res.json(blog);
});
});
});
// Delete
// Create endpoint /api/blogs/:blod_id for Delete
app.delete("/api/blogs/:blog_id", function (req, res) {
Blog.findByIdAndRemove(req.params.blog_id, function (err) {
if (err) {
res.send(err)
}
res.json({ message: "Successfully removed blog." });
});
});
// Load the index file
app.get('*', function (req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.listen(port);
console.log('server listening at port:' + port);