Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
luca3104 committed Feb 22, 2017
1 parent a4e1964 commit 1fc958d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
82 changes: 71 additions & 11 deletions bin/apigen
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,89 @@ 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];
}
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);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 1fc958d

Please sign in to comment.