-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (114 loc) · 4.48 KB
/
server.js
File metadata and controls
140 lines (114 loc) · 4.48 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var Post = require('./models/post.js');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(express.static(__dirname));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost'); // connect to our database
// ROUTES FOR OUR API
// =============================================================================
// get an instance of the express Router
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'Hooray! welcome to our API!' });
});
// more routes for our API will happen here
// on routes that end in /Posts
// ----------------------------------------------------
router.route('/blog')
// create a Post (accessed at POST http://localhost:8080/api/Posts)
.post(function(req, res) {
var post = new Post(); // create a new instance of the Post model
post.title = req.body.title;
post.body = req.body.body; // set the Posts title and body (comes from the request)
// save the Post and check for errors
post.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({ message: 'Post created!' });
}
});
})
// get all the Posts (accessed at GET http://localhost:8080/api/Posts)
.get(function(req, res) {
Post.find(function(err, posts) {
if (err) {
res.send(err);
} else {
res.json(posts);
}
});
});
// on routes that end in /Posts/:Post_id
// ----------------------------------------------------
router.route('/blog/:post_id')
// get the Post with that id (accessed at GET http://localhost:8080/api/Posts/:Post_id)
.get(function(req, res) {
Post.findById(req.params.post_id, function(err, post) {
if (err) {
res.send(err);
} else {
res.json(post);
}
});
})
// update the Post with this id (accessed at PUT http://localhost:8080/api/Posts/:Post_id)
.put(function(req, res) {
// use our Post model to find the Post we want
Post.findById(req.params.post_id, function(err, post) {
if (err) {
res.send(err);
} else {
post.title = req.body.title;
post.body = req.body.body; // update the Posts info
}
// save the Post
post.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({ message: 'Post updated!' });
}
});
});
})
// delete the Post with this id (accessed at DELETE http://localhost:8080/api/Posts/:Post_id)
.delete(function(req, res) {
Post.remove({
_id: req.params.post_id
}, function(err, post) {
if (err) {
res.send(err);
} else {
res.json({ message: 'Successfully deleted' });
}
});
});
//Loads Index when request comes in at localhost:8080
// app.get('*', function(req, res) {
// res.sendfile('index.html'); // load the single view file (angular will handle the page changes on the front-end)
// });
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);