-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (37 loc) · 1.34 KB
/
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
var url = require('url'),
bodyParser = require('body-parser'),
express = require('express'),
Ubiquitary = require('./ubiquitary'),
app = express(),
charactersList = require('./resources/characters.json'),
charactersMap,
port = process.env.PORT || 8080,
router = express.Router();
charactersMap = charactersList
.reduce(function mapper(acc, item) {
acc[item.alias.toLowerCase()] = item;
return acc;
}, {});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
router
.get('/characters', function collectionResponse(req, res) {
var contentType = Ubiquitary.mediaType(req.headers.accept),
result = Ubiquitary.response(contentType, charactersList);
res.setHeader('Content-Type', contentType);
res.statusCode = 200;
res.json(result);
});
router
.get('/characters/:alias', function resourceResponse(req, res) {
var name = req.params.alias.toLowerCase(),
data = JSON.parse(JSON.stringify(charactersMap[name] || '')),
contentType = Ubiquitary.mediaType(req.headers.accept),
result = Ubiquitary.response(contentType, data);
res.setHeader('Content-Type', contentType);
res.statusCode = result ? 200 : 404;
res.json(result || {});
});
app.use('/api', router);
app.listen(port);
console.log('Magic happens on port ' + port);