Skip to content

Commit

Permalink
Added /user POST method
Browse files Browse the repository at this point in the history
  • Loading branch information
RiteshKSingh1709 committed Apr 28, 2018
1 parent ab6d66e commit 104c35d
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 29 deletions.
1 change: 1 addition & 0 deletions nodeREST/.data/test/newfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo":"koo"}
1 change: 1 addition & 0 deletions nodeREST/.data/users/8276079012.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"firstName":"ritesh","lastName":"singh","phone":"8276079012","tosAgreement":true}
44 changes: 17 additions & 27 deletions nodeREST/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
6 changes: 4 additions & 2 deletions nodeREST/config.js → nodeREST/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
109 changes: 109 additions & 0 deletions nodeREST/lib/handlers.js
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 38 additions & 0 deletions nodeREST/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 104c35d

Please sign in to comment.