-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
135 lines (111 loc) · 4.14 KB
/
app.js
File metadata and controls
135 lines (111 loc) · 4.14 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
/**
* Created by marcinlimanski on 25/07/15.
*/
// 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 mongoose = require('mongoose'); //importing the mongoose lib
var cors = require('cors')
//This will allow our server to use the bike model
var Bike = require('./models/bike');
//Connecting to the mongodb database
mongoose.connect('<mongoDB link>');
// 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());
app.use(cors());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
//No longer needed as we use cors lib
/*
// do logging
console.log('Someone made a request.');
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:63342');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
*/
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: 'REST API! ready to use' });
console.log("get request made");
});
//router.rout allows to handle multiple routes for the same URI.
router.route('/bikes')
// create a bike (accessed at POST http://localhost:8080/api/bikes)
.post(function(req, res) {
var bike = new Bike(); // create a new instance of the bike model
bike.name = req.body.name; // set the bikes name (comes from the request)
bike.type = req.body.type;
bike.price = req.body.price;
// save the bike and check for errors
bike.save(function(err) {
if (err)
res.send(err);
else
res.json({ message: 'Bike created!' });
});
})
// get all the bike (accessed at GET http://localhost:8080/api/bikes)
.get(function(req, res) {
Bike.find(function(err, bikes) {
if (err)
res.send(err);
else
res.json(bikes);
});
});
router.route('/bikes/:bike_id')
// get the bike with that id (accessed at GET http://localhost:8080/api/bikes/:bike_id)
.get(function(req, res) {
Bike.findById(req.params.bike_id, function(err, bike) {
if (err)
res.send(err);
else
res.json(bike);
});
})
// update the bike with this id (accessed at PUT http://localhost:8080/api/bikes/:bike_id)
.put(function(req, res) {
// use our bike model to find the bike we want
Bike.findById(req.params.bike_id, function (err, bike) {
if (err)
res.send(err);
else
bike.name = req.body.name;
bike.type = req.body.type;
bike.price = req.body.price;
bike.save(function (err) {
if (err)
res.send(err);
else
res.json({message: 'Bike name updated!'});
});
});
})
// delete the bike with this id
.delete(function(req, res) {
Bike.remove({
_id: req.params.bike_id
}, function(err, bike) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
app.use('/api', router);
// START THE SERVER
app.listen(port);
console.log('Server is listening on port: ' + port);