forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
154 lines (126 loc) · 4.71 KB
/
api-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
145
146
147
148
149
150
151
152
153
154
var fs = require('fs'),
path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
assign = require('lodash/assign'),
find = require('lodash/find'),
findIndex = require('lodash/findIndex'),
app = express(),
router = express.Router(),
// Using a JSON file as our "database"
EMAILS_FILE = path.join(__dirname, 'data/emails.json'),
port = process.env.PORT || 9090;
function getEmails(callback) {
fs.readFile(EMAILS_FILE, function(err, fileContents) {
if (err) {
console.log(err);
process.exit(1);
}
callback(JSON.parse(fileContents));
});
}
function saveEmails(emails, callback) {
fs.writeFile(EMAILS_FILE, JSON.stringify(emails, null, 4), function(err) {
if (err) {
console.log(err);
process.exit(1);
}
callback();
});
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// allow for cross-origin API requests
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
next();
});
// routes that end in /emails
router.route('/emails')
// create an email (accessed via POST to http://localhost:9090/emails)
.post(function(req, res) {
getEmails(function(emails) {
var newEmail = assign({
id: Date.now(),
date: new Date() + '',
unread: true
}, req.body),
newEmails = emails.concat(newEmail);
// write out file back to disk
saveEmails(newEmails, function() {
res.json({success: true});
});
});
})
// get all the emails (access via GET from http://localhost:9090/emails)
.get(function(req, res) {
getEmails(function(emails) {
// Return back the full list of emails
res.setHeader('Cache-Control', 'no-cache');
res.json(
emails
.filter(function(email) { return !email.deleted; })
.sort(function(emailA, emailB) { return new Date(emailB.date) - new Date(emailA.date); })
);
});
});
// routes that end in emails/:emailId
router.route('/emails/:emailId')
// get the email with this id (accessed via GET from http://localhost:9090/emails/:emailId)
.get(function(req, res) {
getEmails(function(emails) {
var emailIdToGet = +req.params.emailId,
emailToGet = find(emails, function(email) {
return email.id === emailIdToGet;
});
res.json(emailToGet);
});
})
// update the email this id (accessed via PUT on http://localhost:9090/emails/:emailId)
.put(function(req, res) {
getEmails(function(emails) {
var emailIdToUpdate = +req.params.emailId,
// make a new copy of the emails list, updating the appropriate email
updatedEmails = emails.map(function(email) {
if (email.id === emailIdToUpdate) {
// make a copy of the email to update before updating
return assign({}, email, {
unread: !!req.body.unread
});
}
return email;
});
saveEmails(updatedEmails, function() {
res.json({success: true});
});
});
})
// delete the email this id (accessed via PUT on http://localhost:9090/emails/:emailId)
.delete(function(req, res) {
getEmails(function(emails) {
var emailIdToDelete = +req.params.emailId,
// make a new copy of the emails list, marking the appropriate email as deleted
updatedEmails = emails.map(function(email) {
if (email.id === emailIdToDelete) {
// make a copy of the email to update before updating
return assign({}, email, {
deleted: true
});
}
return email;
});
saveEmails(updatedEmails, function() {
res.json({success: true});
});
});
});
// Register the routes
app.use('/', router);
app.get('/ping', function(req, res) {
res.json({success: true});
});
app.listen(port, function() {
console.log('Server started: http://localhost:' + port + '/');
});