From 104c35d166ae8ca586f6252ec626c4f64b392719 Mon Sep 17 00:00:00 2001 From: rishu-18 Date: Sat, 28 Apr 2018 21:26:28 +0530 Subject: [PATCH] Added /user POST method --- nodeREST/.data/test/newfile.json | 1 + nodeREST/.data/users/8276079012.json | 1 + nodeREST/index.js | 44 +++++------ nodeREST/{ => lib}/config.js | 6 +- nodeREST/lib/handlers.js | 109 +++++++++++++++++++++++++++ nodeREST/lib/helpers.js | 38 ++++++++++ 6 files changed, 170 insertions(+), 29 deletions(-) create mode 100644 nodeREST/.data/test/newfile.json create mode 100644 nodeREST/.data/users/8276079012.json rename nodeREST/{ => lib}/config.js (85%) create mode 100644 nodeREST/lib/handlers.js create mode 100644 nodeREST/lib/helpers.js diff --git a/nodeREST/.data/test/newfile.json b/nodeREST/.data/test/newfile.json new file mode 100644 index 0000000..a00f177 --- /dev/null +++ b/nodeREST/.data/test/newfile.json @@ -0,0 +1 @@ +{"foo":"koo"} \ No newline at end of file diff --git a/nodeREST/.data/users/8276079012.json b/nodeREST/.data/users/8276079012.json new file mode 100644 index 0000000..d48c1d9 --- /dev/null +++ b/nodeREST/.data/users/8276079012.json @@ -0,0 +1 @@ +{"firstName":"ritesh","lastName":"singh","phone":"8276079012","tosAgreement":true} \ No newline at end of file diff --git a/nodeREST/index.js b/nodeREST/index.js index 3af635b..04745ad 100644 --- a/nodeREST/index.js +++ b/nodeREST/index.js @@ -10,9 +10,11 @@ var StringDecoder = require('string_decoder').StringDecoder; var https = require('https'); var fs = require('fs'); //import config file -var config = require('./config'); +var config = require('./lib/config'); console.log(config); var _data = require('./lib/data'); +var handlers = require('./lib/handlers'); +var helpers = require('./lib/helpers'); // create the HTTP Server var httpServer = http.createServer(function(req,res){ @@ -26,14 +28,14 @@ var DEBUG=true; //Testing the create function if(!DEBUG){ - _data.create('test','newfile','json',function(err){ - console.log("The error is : "+ err); + _data.create('test','newfile',{'foo':'bar'},'json',function(err){ + console.log("The error is : "+ err) }); } //Testing the read function if(DEBUG) { - _data.read('test','newfile',function(err,data){ + _data.read('test','newfile',function(err,data){ if(!err) { console.log(data); } else { @@ -109,16 +111,21 @@ var unifiedServer = function(req,res){ buffer += decoder.end(); //choose the handler this request should go - var choosenHandler = typeof(router[trimmedUrl]) !== 'undefined' ? router[trimmedUrl] : handler.NotFound; - + var choosenHandler = typeof(router[trimmedUrl]) !== 'undefined' ? router[trimmedUrl] : handlers.NotFound; + console.log("The handler is :"+choosenHandler); //condtruct the data object that send to handler var data = { 'trimmedPath' : trimmedUrl, 'queryStringObject' : queryStringObject, 'method' : method, 'headers' : header, - 'payload' : buffer, + 'payload' : helpers.parseJsonToObject(buffer), } + console.log("The data is :"+data.payload.firstName + "The typeof : " + typeof(data.payload.firstName)); + // for(let Obj in data.payload){ + // console.log("coming here"); + // console.log(Obj + " : " + data.payload[Obj] + "typeof(object) : " + typeof(data.payload[Obj])); + // } choosenHandler(data,function(statusCode,payload){ //use the status code called by handler or deault @@ -149,26 +156,9 @@ var unifiedServer = function(req,res){ }; -//Define the handler -var handler = {}; - -//sample handler -handler.sample = function(data,callback){ - callback(200,{'name':'sample handler'}); -}; - -//Ping handler -handler.ping=function(data,callback) -{ - callback(200); -}; - -handler.NotFound = function(data,callback) -{ - callback(404); -}; //setting up the router var router = {}; -router.sample = handler.sample; -router.ping = handler.ping +router.sample = handlers.sample; +router.ping = handlers.ping; +router.users = handlers.users; diff --git a/nodeREST/config.js b/nodeREST/lib/config.js similarity index 85% rename from nodeREST/config.js rename to nodeREST/lib/config.js index ad164f8..1ac5984 100644 --- a/nodeREST/config.js +++ b/nodeREST/lib/config.js @@ -9,14 +9,16 @@ var envrionments = {}; envrionments.staging = { 'httpPort' : 3000, 'httpsPort' : 3001, - 'envName' : 'staging' + 'envName' : 'staging', + 'hashingSecret' : 'thisIsSecret' } //envrionment specific to production envrionments.production = { 'httpPort' : 5000, 'httpsPort' : 5001, - 'envName' : 'production' + 'envName' : 'production', + 'hashingSecret' : 'thisIsAlsoSecret' } //get the current envrionment using command line argument diff --git a/nodeREST/lib/handlers.js b/nodeREST/lib/handlers.js new file mode 100644 index 0000000..3a8c96e --- /dev/null +++ b/nodeREST/lib/handlers.js @@ -0,0 +1,109 @@ +/* +* Request handlers +* +*/ + +//Dependecies +var _data = require('./data'); +var helpers = require('./helpers'); +//Define the handler +var handlers = {}; + +//Users +handlers.users = function(data,callback){ + var acceptableMethods = ['post','get','put','delete']; + if(acceptableMethods.indexOf(data.method) > -1){ + handlers._users[data.method](data,callback); + } else { + // returning 405 that method is not available + callback(405); + } +}; + +//Container for user submethods +handlers._users = {}; + +// Users - POST +// Required data firstName,lastName,Phn Number,password,tosAgreement +handlers._users.post = function(data,callback){ + //Check all the required field are filled out + var firstName = typeof(data.payload.firstName) == 'string' && data.payload.firstName.trim().length > 0 ? data.payload.firstName.trim() : false; + var lastName = typeof(data.payload.lastName) == 'string' && data.payload.lastName.trim().length > 0 ? data.payload.lastName.trim() : false; + var phone = typeof(data.payload.phone == 'string') && data.payload.phone.length == 10 ? data.payload.phone : false; + var password = typeof(data.payload.password == 'string') && data.payload.password.length > 0 ? data.payload.password : false; + var tosAgreement = typeof(data.payload.tosAgreement == 'boolean') && data.payload.tosAgreement == true ? true : false; + + if(firstName&& lastName && phone && password && tosAgreement){ + //Make sure that user doesnot exists + _data.read('users',phone,function(err,data){ + if(err) { + // Hash the password + var hashedPassword = helpers.hash(password); + + //sanity check for hashedPassword + if(hashedPassword){ + //Create User Object + var userObject = { + 'firstName' : firstName, + 'lastName' : lastName, + 'phone' : phone, + 'tosAgreement' : true, + } + + _data.create('users',phone,userObject,function(err){ + if(!err) { + callback(200); + } else { + + callback(500,{'Error' : 'Couldnot create the new user'}); + } + }); + } else { + + callback(500,{'Error': 'Could not create the hashedPassword'}); + } + + + } else { + callback(400,{'Error' : 'A user with that phone number already exists'}); + } + }); + + } else { + callback(400,{'Error' : 'Missing required fields'}); + } +}; + +//Users - GET +handlers._users.get = function(data,callback){ + +}; + +//Users - PUT +handlers._users.put = function(data,callback){ + +}; + +//User -DELETE +handlers._users.delete = function(data,callback){ + +}; + +//sample handler +handlers.sample = function(data,callback){ + callback(200,{'name':'sample handler'}); +}; + +//Ping handler +handlers.ping=function(data,callback) +{ + callback(200); +}; + +handlers.NotFound = function(data,callback) +{ + callback(404); +}; + +//Export the module +module.exports = handlers; \ No newline at end of file diff --git a/nodeREST/lib/helpers.js b/nodeREST/lib/helpers.js new file mode 100644 index 0000000..9f55c45 --- /dev/null +++ b/nodeREST/lib/helpers.js @@ -0,0 +1,38 @@ +/* +* Helpers function for our library +* +*/ + +//Dependencies +var crypto = require('crypto'); +var config = require('./config'); + +// Container for the all the helpers +var helpers = {}; + +// create a SHA256 hash +helpers.hash = function(str) { + if(typeof(str)=='string' && str.length > 0){ + var hash = crypto.createHmac('sha256',config.hashingSecret).update(str).digest('hex'); + return hash; + } else { + return false; + } +}; + +//Parse a JSON string to object in all cases +helpers.parseJsonToObject = function(str){ + console.log("coming here"); + try{ + console.log("coming here"+str); + var obj = JSON.parse(str); + return obj; + } catch(e){ + console.log("coming here.." +e); + return {}; + } +}; + + +// Exports the module +module.exports = helpers; \ No newline at end of file