-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (114 loc) · 3.82 KB
/
app.js
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
// modules
var express = require('express');
var app = express();
var mongoose = require('mongoose');
//var Schema = mongoose.Schema;
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
// config files
//var db = require('./config/db');
// port
var port = process.env.PORT || 8080;
//connect to database (without /config/db.js credentials)
mongoose.connect('mongodb://localhost/test');
var Apartment = require('./app/models/apartments');
// create some apartments ========================================
var populate = require('./populate');
populate.createApartments();
// get all data/stuff of the body (POST) parameters===============
// parse application/json
app.use(bodyParser.json());
// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
app.use(methodOverride('X-HTTP-Method-Override'));
// set the static files location /public/img will be /img for users
app.use(express.static(__dirname + '/public'));
// routes ==================================================
//require('./app/routes')(app); // configure our routes
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
//console.log('Something is happening.');
next();
});
// 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!' });
});
// on routes that end in /apartments
// ----------------------------------------------------
router.route('/apartments')
// create an apartment (accessed at POST http://localhost:8080/apartments)
.post(function(req, res) {
var apt = new Apartment();
apt.address = req.body.address;
apt.price = req.body.price;
apt.bedrooms = req.body.bedrooms;
apt.pets = req.body.pets;
apt.laundry = req.body.laundry;
apt.dishwasher = req.body.dishwasher;
apt.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Apartment created!' });
});
})
// get all the apartments (accessed at GET http://localhost:8080/api/apartments)
.get(function(req, res) {
Apartment.find(function(err, apts) {
if (err)
res.send(err);
//console.log('Get!');
res.json(apts);
});
});
// on routes that end in /apartments/:apt_id
// ----------------------------------------------------
router.route('/apartments/:apt_id')
// get apt with that id
.get(function(req, res) {
Apartment.findById(req.params.apt_id, function(err, apt) {
if (err)
res.send(err);
res.json(apt);
});
})
// update apt with this id
.put(function(req, res) {
Apartment.findById(req.params.apt_id, function(err, apt) {
if (err)
res.send(err);
apt.address = req.body.address;
apt.price = req.body.price;
apt.bedrooms = req.body.bedrooms;
apt.pets = req.body.pets;
apt.laundry = req.body.laundry;
apt.dishwasher = req.body.dishwasher;
/*date and photo here*/
apt.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Apartment updated!' });
});
});
})
// delete apt with this id
.delete(function(req, res) {
Apartment.remove({
_id: req.params.apt_id
}, function(err, apt) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
app.use('/api', router);
// start app ===============================================
// startup our app at http://localhost:8080
app.listen(port);
// expose app
//exports = module.exports = app;