From ab0a5a65a63e62cec3d51cd799096cdd97c9018b Mon Sep 17 00:00:00 2001 From: tshemsedinov Date: Fri, 8 Jun 2018 23:50:54 +0300 Subject: [PATCH 1/2] Add api proxy/stub auto building --- JavaScript/5-api.js | 41 +++++++++++++++++++++++++++++++++++++++++ JavaScript/5-server.js | 27 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 JavaScript/5-api.js create mode 100644 JavaScript/5-server.js diff --git a/JavaScript/5-api.js b/JavaScript/5-api.js new file mode 100644 index 0000000..a8fb98d --- /dev/null +++ b/JavaScript/5-api.js @@ -0,0 +1,41 @@ +'use strict'; + +const http = require('http'); + +const ajax = (base, methods) => { + const api = {}; + for (const method of methods) { + api[method] = (...args) => { + const callback = args.pop(); + const url = base + method + '/' + args.join('/'); + console.log(url); + http.get(url, res => { + if (res.statusCode !== 200) { + callback(new Error(`Status Code: ${res.statusCode}`)); + return; + } + const lines = []; + res.on('data', chunk => lines.push(chunk)); + res.on('end', () => callback(null, JSON.parse(lines.join()))); + }); + }; + } + return api; +}; + +// Usage + +const api = ajax( + 'http://localhost:8000/api/', + ['user', 'userBorn'] +); + +api.user('marcus', (err, data) => { + if (err) throw err; + console.dir({ data }); +}); + +api.userBorn('mao', (err, data) => { + if (err) throw err; + console.dir({ data }); +}); diff --git a/JavaScript/5-server.js b/JavaScript/5-server.js new file mode 100644 index 0000000..b4843d1 --- /dev/null +++ b/JavaScript/5-server.js @@ -0,0 +1,27 @@ +'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 +}; + +const types = { + object: o => JSON.stringify(o), + undefined: () => '{"error":"not found"}', + function: (fn, req, res) => JSON.stringify(fn(req, res)) +}; + +http.createServer((req, res) => { + const data = routing[req.url]; + const type = typeof(data); + const serializer = types[type]; + const result = serializer(data, req, res); + res.end(result); +}).listen(8000); From 1283715cd5205c90c3c400e6bbcd306aa219a929 Mon Sep 17 00:00:00 2001 From: tshemsedinov Date: Sat, 9 Jun 2018 13:04:31 +0300 Subject: [PATCH 2/2] Simplify server for demo purposes --- JavaScript/5-server.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/JavaScript/5-server.js b/JavaScript/5-server.js index b4843d1..d033718 100644 --- a/JavaScript/5-server.js +++ b/JavaScript/5-server.js @@ -8,20 +8,14 @@ const users = { }; const routing = { - '/api/user/': name => users[name], - '/api/userBorn/': name => users[name].born -}; - -const types = { - object: o => JSON.stringify(o), - undefined: () => '{"error":"not found"}', - function: (fn, req, res) => JSON.stringify(fn(req, res)) + '/api/user': name => users[name], + '/api/userBorn': name => users[name].born }; http.createServer((req, res) => { - const data = routing[req.url]; - const type = typeof(data); - const serializer = types[type]; - const result = serializer(data, req, res); - res.end(result); + 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);