-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (65 loc) · 1.97 KB
/
server.js
File metadata and controls
75 lines (65 loc) · 1.97 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
///MODULES///
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var mongoose = require('mongoose');
var methodOverride = require('method-override');
///********///
///EXTERNAL FILES///
var Customer = require('./models/customer-model.js');
///**************///
///EXPRESS CONFIG///
app.use(methodOverride('_method'));
app.use(express.static(__dirname + '/public'));
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(cors());
///***********///
///MONGOOSE///
var mongoURI = process.env.MONGODB_URI || 'mongodb://localhost/customers';
mongoose.connect(mongoURI);
mongoose.Promise = global.Promise;
///********///
app.get('/customers', (req, res) => {
Customer.find({}, (err, customers) => {
if(err) console.log(err);
console.log("Customers >>>>>>>", customers);
return customers;
})
.then((customers) => {
res.json(customers);
});
});
app.post('/customers', (req, res) => {
var customer = new Customer(req.body);
customer.save((err, customer) => {
if(err) console.log(err);
console.log("Customer Created >>>>>>", customer);
res.json(customer);
});
});
app.put('/customers/:id', (req, res) => {
Customer.findOneAndUpdate({"_id": req.params.id}, req.body, (err, customer) => {
if(err) console.log(err);
console.log("Customer Updated >>>>", customer);
res.json(customer);
});
});
app.delete('/customers/:id', (req, res) => {
Customer.findByIdAndRemove(req.params.id, (err, customer) => {
if(err) console.log(err);
console.log("Customer Deleted >>>>", customer);
res.json(customer);
});
});
///SERVER///
app.listen(process.env.PORT || 3000, () => {
console.log('=============================');
console.log('SERVER CONNECTED TO PORT 3000');
console.log('=============================');
});
///********///