-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (92 loc) · 3.28 KB
/
server.js
File metadata and controls
108 lines (92 loc) · 3.28 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
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
/**
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import fs from 'fs';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
const staticRouter = express.Router();
const apiRouter = express.Router();
const COMMENTS_FILE = path.join(__dirname, 'comments.json');
const INDEX = fs.readFileSync(path.join(__dirname, 'public', 'index.html'), {encoding: 'utf8'});
app.set('port', (process.env.PORT || 4000));
const notFound = (req, res, next) => {
res.status(404).send(`${req.originalUrl} Not Found.`);
};
const errorHandler = (err, req, res, next) => {
res.status(500).send('Server error');
};
staticRouter.use('/', express.static(path.join(__dirname, 'public')));
staticRouter.use(/^\/(css|scripts)/, notFound);
apiRouter.use(bodyParser.json());
apiRouter.use(bodyParser.urlencoded({
extended: true
}));
apiRouter.get('/comments', (req, res) =>
fs.readFile(COMMENTS_FILE, (err, data) => {
if (err) {
console.error(err);
process.exit(1);
}
res.setHeader('Cache-Control', 'no-cache');
res.json(JSON.parse(data));
})
);
apiRouter.post('/comments', (req, res) =>
fs.readFile(COMMENTS_FILE, (err, data) => {
if (err) {
console.error(err);
process.exit(1);
}
let comments = JSON.parse(data);
// NOTE: In a real implementation, we would likely rely on a database or
// some other approach (e.g. UUIDs) to ensure a globally unique id. We'll
// treat Date.now() as unique-enough for our purposes.
const newComment = {
id: Date.now(),
author: req.body.author,
text: req.body.text,
};
comments = comments.concat(newComment);
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4), err => {
if (err) {
console.error(err);
process.exit(1);
}
res.setHeader('Cache-Control', 'no-cache');
res.json(newComment);
});
})
);
apiRouter.get('/authors/:author', (req, res) => {
console.log('author: ', req.params.author);
fs.readFile(COMMENTS_FILE, (err, data) => {
if (err) {
console.error(err);
process.exit(1);
}
res.setHeader('Cache-Control', 'no-cache');
res.json(
JSON.parse(data).filter(comment => comment.author === req.params.author)
);
});
});
apiRouter.use(notFound);
app.use('/', staticRouter);
app.use('/api', apiRouter);
app.use('/', (req, res) => {
res.send(INDEX)
});
app.use('/', errorHandler);
app.listen(app.get('port'), function() {
console.log(`Server started: http://localhost:${app.get('port')}/`);
});