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/*
*
*
*/
// 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 student = require('./student.js');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 16386; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// middle-ware 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:16386/api)
router.get('/', function(req, res) {
// Logic to show student here
res.json({ message: 'Welcome to our student Instance 1 api!!' });
});
// more routes for our API will happen here
/************************************************************
*
* Student API Endpoints
*
*************************************************************/
//API endpoint to add student to the students table
router.route('/student')
// create a new student (accessed at POST http://localhost:16386/api/student)
.post(function(req, res) {
student.addStudent(req);
res.json({message : 'Successfully added student'});
console.log("Request handled");
});
//API end point to get student details (accessed at GET http://localhost:16386/api/student/id)
router.route('/student/:student_id')
// get the student with that id (accessed at GET http://localhost:16386/api/student/:student_id)
.get(function(req, res) {
// Logic to show student here
student.getStudentDetails(req,res,handleResult);
function handleResult(response, err)
{
if(err)
{
console.error(err.stack || err.message);
return;
}
res.json(response.body);
console.log("Request handled");
}
})
.delete(function(req, res) {
student.deleteStudent(req);
res.json({ message: 'Student details from Student Instance 1 deleted!' });
})
// Logic to show student here
// res.json({ message: 'Student details from Student Instance 1!' });
// update the student with this id (accessed at PUT http://localhost:16386/api/student/:student_id)
.put(function(req, res) {
//Logic to update student details here
student.updateStudent(req);
res.json({ message: 'Student updated!' });
});
// 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);