-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
146 lines (122 loc) · 3.86 KB
/
server.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
/**
* Created by HOME-PC on 28.02.15.
*/
// Module dependencies.
var application_root = __dirname,
express = require( 'express' ), //Web framework
bodyParser = require('body-parser'), //Parser for reading request body
path = require( 'path' ), //Utilities for dealing with file paths
mongoose = require( 'mongoose' ); //MongoDB integration
//Create server
var app = express();
////Connect to database
//var db = mongoose.connection;
//
//db.on('error', console.error);
//db.once('open', function() {
// // Create your schemas and models here.
//});
mongoose.connect('mongodb://localhost/phoneApp1');
var db = mongoose.connection;
//Where to serve static content
app.use( express.static( path.join( application_root, 'public') ) );
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//Schemas
var Contact = {
favorite: Boolean,
firstName: String,
lastName: String,
mobilePhone: Number,
homePhone: Number
};
//Models
var ContactsModel = mongoose.model( 'Contact', Contact);
//Start server
var port = 5000;
app.listen( port, function() {
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
app.get( '/contacts', function( request, response ) {
return ContactsModel.find(function( err, contacts ) {
if( !err ) {
return response.send( contacts );
} else {
return console.log( err );
}
});
});
app.get( '/favorites', function(request, response ) {
return ContactsModel.find({favorite:true},function( err, contacts ) {
if( !err ) {
return response.send( contacts );
} else {
return console.log( err );
}
});
});
app.post( '/contacts', function( request, response ) {
var contact = new ContactsModel({
favorite : request.body.favorite,
firstName: request.body.firstName,
lastName: request.body.lastName,
mobilePhone: request.body.mobilePhone,
homePhone: request.body.homePhone
});
return contact.save( function( err ) {
if( !err ) {
console.log( 'created' );
return response.send( contact );
} else {
console.log( err );
response.status(422);
return response.send( err );
}
});
});
app.get( '/contacts/:id', function( request, response ) {
return ContactsModel.findById( request.params.id, function( err, contact ) {
if( !err ) {
return response.send( contact );
} else {
return console.log( err );
}
});
});
var putFunc = function( request, response ) {
console.log(request.params);
return ContactsModel.findById(request.params.id, function( err, contact ) {
contact.favorite = request.body.favorite;
contact.firstName = request.body.firstName;
contact.lastName = request.body.lastName;
contact.mobilePhone = request.body.mobilePhone;
contact.homePhone = request.body.homePhone;
return contact.save( function( err ) {
if( !err ) {
console.log( 'contact updated' );
return response.send( contact );
} else {
console.log( err );
}
});
});
};
app.put( '/contacts/:id', putFunc);
app.put( '/favorites/:id', putFunc);
var deleteFunc = function( request, response ) {
console.log( 'Deleting contact with id: ' + request.params.id );
return ContactsModel.findById( request.params.id, function( err, model ) {
return model.remove( function( err ) {
if( !err ) {
console.log( 'Contact removed' );
return response.send(model);
} else {
console.log( err );
}
});
});
};
app.delete( '/contacts/:id', deleteFunc);
app.delete( '/favorites/:id', deleteFunc);