forked from BastianHofmann/cloud-computing-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·154 lines (139 loc) · 3.77 KB
/
server.js
File metadata and controls
executable file
·154 lines (139 loc) · 3.77 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
144
145
146
147
148
149
150
151
152
153
154
// require express and other modules
const express = require('express');
const app = express();
// Express Body Parser
app.use(express.urlencoded({extended: true}));
app.use(express.json());
var Books = require('./models/books');
// Set Static File Directory
app.use(express.static(__dirname + '/public'));
/************
* DATABASE *
************/
const db = require('./models');
/**********
* ROUTES *
**********/
/*
* HTML Endpoints
*/
app.get('/', function homepage(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
/*
* JSON API Endpoints
*/
app.get('/api', (req, res) => {
// TODO: Document all your api endpoints below as a simple hardcoded JSON object.
res.json({
message: 'Welcome to my app api!',
documentationUrl: '', //leave this also blank for the first exercise
baseUrl: '', //leave this blank for the first exercise
endpoints: [
{method: 'GET', path: '/api', description: 'Describes all available endpoints'},
{method: 'GET', path: '/api/profile', description: 'Data about me'},
{method: 'GET', path: '/api/books/', description: 'Get All books information'},
{method: 'POST', path: '/api/books/', description: 'Add new book information'},
{method: 'PUT', path: '/api/books/:id', description: 'Update book information via id'},
{method: 'DELETE', path: '/api/books/:id', description: 'Delete book information via id'}
// TODO: Write other API end-points description here like above
]
})
});
// TODO: Fill the values
app.get('/api/profile', (req, res) => {
res.json({
'name': req.body.name,
'homeCountry': req.body.homeCountry,
'degreeProgram': 'informatics',//informatics or CSE.. etc
'email': req.body.email,
'deployedURLLink': '',//leave this blank for the first exercise
'apiDocumentationURL': '', //leave this also blank for the first exercise
'currentCity': req.body.currentCity,
'hobbies': []
})
});
/*
* Get All books information
*/
app.get('/api/books/', (req, res) => {
/*
* use the books model and query to mongo database to get all objects
*/
db.books.find({}, function (err, books) {
if (err) throw err;
/*
* return the object as array of json values
*/
res.json(books);
});
});
/*
* Add a book information into database
*/
app.post('/api/books/', (req, res) => {
/*
* New Book information in req.body
*/
console.log(req.body);
/*
* use the books model and create a new object
* with the information in req.body
*/
let book = req.body;
Books.create({
title: book.title,
author: book.author,
releaseDate: book.releaseDate,
genre: book.genre,
rating: book.rating,
language: book.language
}, function(err, book) {
if (err)
res.send(err);
Books.find(function(err, book) {
if (err)
res.send(err)
res.json(book);
});
});
/*
* return the new book information object as json
*/
});
/*
* Update a book information based upon the specified ID
*/
app.put('/api/books/:id', (req, res) => {
/*
* Get the book ID and new information of book from the request parameters
*/
Books.updateOne({_id:req.params.id}, req.body, function (err, place) {
res.send(place);
});
});
/*
* Delete a book based upon the specified ID
*/
app.delete('/api/books/:id', (req, res) => {
/*
* Get the book ID of book from the request parameters
*/
console.log(req.params._id);
let id = req.params.id;
Books.deleteOne({ _id: id });
Books.remove({ _id : id},
function(err) {
if (err)
res.send(err);
else
res.send('Successfully! Employee has been Deleted.');
});
});
/**********
* SERVER *
**********/
// listen on the port 3000
app.listen(process.env.PORT || 80, () => {
console.log('Express server is up and running on http://localhost:80/');
});