-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (108 loc) · 4.03 KB
/
server.js
File metadata and controls
143 lines (108 loc) · 4.03 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
141
142
143
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
var port = process.env.PORT || 8080;
// ROUTES FOR OUR API
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
console.log('Welcome to my school portal.');
next();
});
router.get('/', function(req, res) {
res.sendfile('public/main.html');
});
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
app.listen(port);
console.log('Listening at port ' + port);
//database setup
var mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://school:password@ds117625.mlab.com:17625/school_project', {
socketTimeoutMS: 3000,
keepAlive: true,
reconnectTries: 30
})
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection eroor:'))
conn.once('open', function(){
})
var Student = require('./models/student');
router.route('/student')
// create a bear (accessed at POST http://localhost:8080/api/students)
.post(function(req, res) {
console.log("I am Testing Post")
var student = new Student();
student.first_name = req.body.first_name;
student.last_name = req.body.last_name;
student.phone = req.body.phone;
student.age = req.body.age;
student.school = req.body.school;
student.course = req.body.course
console.log(student);
student.save(function(err) {
if (err){
console.log("Another post test")
res.send(err, "here");
}
else{
console.log("Another post test")
res.json({ message: 'School created!' });
}
});
console.log("Another post")
});
router.route('/school/:student_id')
// get the bear with that id (accessed at GET http://localhost:8080/api/bears/:bear_id)
.get(function(req, res) {
Student.findById(req.params.student_id, function(err, student) {
if (err)
res.send(err);
res.json(student);
});
})
// update the studeents with this id (accessed at PUT http://localhost:8080/api/students/:student_id)
.put(function(req, res) {
Student.findById(req.params.student_id, function(err, student) {
if (err)
res.send(err);
student.first_name = req.body.first_name;
student.last_name = req.body.last_name;
student.phone = req.body.phone;
student.age = req.body.age;
student.school = req.body.school;
student.course = req.body.course
student.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'School updated!' });
});
});
})
// delete the student with this id (accessed at DELETE http://localhost:8080/api/students/:student_id)
.delete(function(req, res) {
Student.remove({
_id: req.params.student_id
}, function(err, student) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
router.route('/students')
// get all the students (accessed at GET http://localhost:8080/api/students)
.get(function(req, res) {
Student.find(function(err, student) {
if (err)
res.send(err);
res.json(student);
});
});
app.use('/api', router);