-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path5-server.js
More file actions
21 lines (17 loc) · 543 Bytes
/
Copy path5-server.js
File metadata and controls
21 lines (17 loc) · 543 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'use strict';
const http = require('http');
const users = {
marcus: { name: 'Marcus Aurelius', city: 'Rome', born: 121 },
mao: { name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
};
const routing = {
'/api/user': name => users[name],
'/api/userBorn': name => users[name].born
};
http.createServer((req, res) => {
const url = req.url.split('/');
const par = url.pop();
const method = routing[url.join('/')];
const result = method ? method(par) : { error: 'not found' };
res.end(JSON.stringify(result));
}).listen(8000);