From 1fc958d0baa7b87ec18f00035d449de554ba62dc Mon Sep 17 00:00:00 2001 From: luca3104 Date: Wed, 22 Feb 2017 18:41:07 +0900 Subject: [PATCH] 0.0.3 --- bin/apigen | 82 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 2 +- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/bin/apigen b/bin/apigen index b9bc989..3d7c428 100755 --- a/bin/apigen +++ b/bin/apigen @@ -30,6 +30,7 @@ const fs = require('fs'); const http = require('http'); var host = 3104; +var header = 200 var server = http.createServer(); if(process.argv[3] !== void 0){ host = process.argv[3]; @@ -37,22 +38,81 @@ if(process.argv[3] !== void 0){ server.on('request', function (req, res) { var json = JSON.parse(fs.readFileSync('./'+process.argv[2], 'utf8')); var key = req.url.replace(/^\u002f/g, ""); - res.writeHead(200, { - 'Content-Type':'application/json', - 'Connection':'close' - }); if(json[key]){ - var resPrams = JSON.stringify(json[key]); - console.log(new Date() + ", " + req.method + "=" + key + ", response=" + resPrams); - res.write(resPrams); - res.end(); + if('status' in json[key]){ + var statusArr = json[key]["status"]; + var success = 200; + var error = 400; + statusArr.forEach(function(status) { + if(status >= 200 && status < 300){ + success = status + header = success; + }else{ + error = status; + } + }); + var requireArr = json[key]["require"]; + var data = ''; + req.on('data', function (chunk) { + data += chunk; + }) + req.on('end', function () { + var match = true; + var dataObj = JSON.parse(data); + var resPrams; + for (var require in requireArr) { + if(requireArr[require] in dataObj){ + }else{ + match = false + header = error + } + } + + if(match == true && String(success) in json[key]){ + resPrams = JSON.stringify(json[key][success]); + } else if (match == false && String(error) in json[key]) { + resPrams = JSON.stringify(json[key][error]); + } else { + header = 404; + resPrams = JSON.stringify({"error":"status does not exist"}); + } + + res.writeHead(header, { + 'Content-Type':'application/json', + 'Connection':'close' + }); + console.log(new Date() + ", " + req.method + "=" + key + ", status="+header+ ", response=" + resPrams); + res.write(resPrams); + res.end(); + }) + } else { + res.writeHead(header, { + 'Content-Type':'application/json', + 'Connection':'close' + }); + resPrams = JSON.stringify(json[key]); + console.log(new Date() + ", " + req.method + "=" + key + ", status="+header+ ", response=" + resPrams); + res.write(resPrams); + res.end(); + } }else{ - console.log(new Date() + ", " + req.method + "=" + key + ", response={}"); - res.write(JSON.stringify({})); + var resPrams; + header = 404; + if("404" in json){ + resPrams = JSON.stringify(json["404"]); + }else{ + resPrams = JSON.stringify({"error":"error"}); + } + res.writeHead(header, { + 'Content-Type':'application/json', + 'Connection':'close' + }); + console.log(new Date() + ", " + req.method + "=" + key + ", status=404, response="+resPrams); + res.write(resPrams); res.end(); } }) server.listen(host, function () { - console.log('listening on ' + host); + console.log('apigen listening on ' + host); }); diff --git a/package.json b/package.json index e36ef64..f71ea34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apigen", - "version": "0.0.2", + "version": "0.0.3", "description": "Node.js RESTful API Generator", "bin": { "apigen": "api-gen.js"