Skip to content

Commit 1283715

Browse files
committed
Simplify server for demo purposes
1 parent ab0a5a6 commit 1283715

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

JavaScript/5-server.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ const users = {
88
};
99

1010
const routing = {
11-
'/api/user/': name => users[name],
12-
'/api/userBorn/': name => users[name].born
13-
};
14-
15-
const types = {
16-
object: o => JSON.stringify(o),
17-
undefined: () => '{"error":"not found"}',
18-
function: (fn, req, res) => JSON.stringify(fn(req, res))
11+
'/api/user': name => users[name],
12+
'/api/userBorn': name => users[name].born
1913
};
2014

2115
http.createServer((req, res) => {
22-
const data = routing[req.url];
23-
const type = typeof(data);
24-
const serializer = types[type];
25-
const result = serializer(data, req, res);
26-
res.end(result);
16+
const url = req.url.split('/');
17+
const par = url.pop();
18+
const method = routing[url.join('/')];
19+
const result = method ? method(par) : { error: 'not found' };
20+
res.end(JSON.stringify(result));
2721
}).listen(8000);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy