-
Notifications
You must be signed in to change notification settings - Fork 0
/
test7_server.js
120 lines (111 loc) · 3.2 KB
/
test7_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
var express = require('express'),
app = express();
//Express 3
//app.configure(function() {
// app.use(express.static(__dirname, '/'));
//});
//Express 4
app.use(express.static(__dirname, '/'));
app.get('/customers/:id', function(req, res) {
var customerId = parseInt(req.params.id);
var data = {};
for (var i=0,len=customers.length;i<len;i++) {
if (customers[i].id === customerId) {
data = customers[i];
break;
}
}
res.json(data);
});
app.get('/customers', function(req, res) {
res.json(customers);
//res.json(500, { error: 'An error has occurred!' });
});
app.get('/orders', function(req, res) {
var orders = [];
for (var i=0,len=customers.length;i<len;i++) {
if (customers[i].orders) {
for (var j=0,ordersLen=customers[i].orders.length;j<ordersLen;j++) {
orders.push(customers[i].orders[j]);
}
}
}
res.json(orders);
});
app.delete('/customers/:id', function(req, res) {
var customerId = parseInt(req.params.id);
var data = { status: true };
for (var i=0,len=customers.length;i<len;i++) {
if (customers[i].id === customerId) {
customers.splice(i,1);
data = { status: true };
break;
}
}
res.json(data);
});
app.listen(8080);
console.log('Express listening on port 8080');
var customers = [
{
id: 1,
joined: '2000-12-02',
name:'John',
city:'Chandler',
orderTotal: 9.9956,
orders: [
{
id: 1,
product: 'Shoes',
total: 9.9956
}
]
},
{
id: 2,
joined: '1965-01-25',
name:'Zed',
city:'Las Vegas',
orderTotal: 19.99,
orders: [
{
id: 2,
product: 'Baseball',
total: 9.995
},
{
id: 3,
product: 'Bat',
total: 9.995
}
]
},
{
id: 3,
joined: '1944-06-15',
name:'Tina',
city:'New York',
orderTotal:44.99,
orders: [
{
id: 4,
product: 'Headphones',
total: 44.99
}
]
},
{
id: 4,
joined: '1995-03-28',
name:'Dave',
city:'Seattle',
orderTotal:101.50,
orders: [
{
id: 5,
product: 'Kindle',
total: 101.50
}
]
}
];