forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
158 lines (135 loc) · 3.95 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
155
156
157
158
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const assign = require('lodash/assign');
const app = express();
const router = express.Router();
// Using a JSON file as our "database"
const EMAILS_FILE = path.join(__dirname, 'data/emails.json');
const {PORT: port = 9090} = process.env;
const getEmails = callback => {
fs.readFile(EMAILS_FILE, (err, fileContents) => {
if (err) {
console.log(err);
process.exit(1);
}
callback(JSON.parse(fileContents));
});
};
const saveEmails = (emails, callback) => {
fs.writeFile(EMAILS_FILE, JSON.stringify(emails, null, 4), 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((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((req, res) => {
getEmails(emails => {
let newEmail = assign(
{
id: Date.now(),
date: new Date() + '',
unread: true
},
req.body
);
let newEmails = [...emails, newEmail];
// write out file back to disk
saveEmails(newEmails, () => {
res.json({success: true});
});
});
})
// get all the emails (access via GET from http://localhost:9090/emails)
.get((req, res) => {
getEmails(emails => {
// Return back the full list of emails
res.setHeader('Cache-Control', 'no-cache');
res.json(
emails
.filter(email => !email.deleted)
.sort(
(emailA, emailB) => 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((req, res) => {
getEmails(emails => {
let emailIdToGet = +req.params.emailId;
let emailToGet = emails.find(email => email.id === emailIdToGet);
res.json(emailToGet);
});
})
// update the email this id (accessed via PUT on http://localhost:9090/emails/:emailId)
.put((req, res) => {
getEmails(emails => {
let emailIdToUpdate = +req.params.emailId;
// make a new copy of the emails list, updating the appropriate email
let updatedEmails = emails.map(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, () => {
res.json({success: true});
});
});
})
// delete the email this id (accessed via PUT on http://localhost:9090/emails/:emailId)
.delete((req, res) => {
getEmails(emails => {
let emailIdToDelete = +req.params.emailId;
// make a new copy of the emails list, marking the appropriate email as deleted
let updatedEmails = emails.map(email => {
if (email.id === emailIdToDelete) {
// make a copy of the email to update before updating
return assign({}, email, {
deleted: true
});
}
return email;
});
saveEmails(updatedEmails, () => {
res.json({success: true});
});
});
});
// Register the routes
app.use('/', router);
app.get('/ping', (req, res) => {
res.json({success: true});
});
app.listen(port, () => {
console.log(`Server started: http://localhost:${port}/`);
});