Hacken is a node module to help people in hackathons, it has almost all the common features a person needs from database operations to sockets. If you are the one who struglles with MongoDb ObjectId, hacken makes it easier to work with then too.
- Object Functions
- JWT Functions
- MongoDb Connection
- User Functions
- Create Docker Image
- Socket Function
To Check if ObjectId is valid.
var hacken = require('hacken');
if(hacken.isObjValid(objid)){
console.log("Valid.");
}
else{
console.log("Invalid.");
}
To convert a ObjectId string to underlying ObjectId.
var hacken = require('hacken');
try{
var objid = hacken.toObjId(req.body.id);
}
catch(e){
console.log(e.err);
}
To get some specific values of properties from an array of objects.
var hacken = require('hacken');
var arr = [{ele1:"one",ele2:"two"},{ele1:"1",ele2:"2"}];
var prop = ["ele1"]; //specify properties which you want to get from an array.
var res = hacken.arrObjMap(arr,prop); //res = ["one","1"];
To Issue A Json Web Token.
var hacken = require('hacken');
var data = "hello hacken";
var secret = "adsasdAFD5454asdasd5Basasdajsdb46555654d656464N15465as4d6546546a";
var time = "5d"; //time can be given in 'd' for days without any suffix means seconds eg: 1100 for 1100 seconds.
try{
hacken.issueToken(data,secret,time).then((token)=>{
console.log(token);
},(err)=>{
console.log(err);
});
}
catch(e){
console.log(e);
}
To Verify a Json Web Token.
var hacken = require('hacken');
var token = req.params.token;
var secret = "adsasdAFD5454asdasd5Basasdajsdb46555654d656464N15465as4d6546546a"; //same as the one provided before.
try{
hacken.verifyToken(token,secret).then((result)=>{
var msg = result.msg;
var decodedData = result.decoded
console.log(msg);
console.log(decodedData);
}).catch((err)=>{
console.log(err);
})
}
catch(e){
console.log(e);
}
To Decode the token.
var hacken = require('hacken');
try{
hacken.decodeToken(token).then((res)=>{
console.log(res);
},(err)=>{
console.log(err);
});
}
catch(err){
console.log(err);
}
To Connect To MongoDb.
var hacken = require('hacken');
var url = "mongodb://localhost:27017/db"; //replace with your url
hacken.mongoConnect(url).then((msg)=>{
console.log(msg);
}).catch((err)=>{
console.log(err);
})
Following Properties Are allowed in User Model:
- username
- password
- phoneno
- name
- age
- address
var object = {username:"value",password:"value",phoneno:"value",email:"value",name:"value",age:"value",address:"value"};
//These objects can also take subset of the above mentioned properties.
//Example:
var object = {username:"value",password:"value",phoneno:"value"};
To Create The User.
var hacken = require('hacken');
hacken.userCreate(object).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Remove the user.
var hacken = require('hacken');
hacken.userRemove(req.body.username).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Log user in.
var hacken = require('hacken');
hacken.userLogin(username,password).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Update The User, Requires the same object with properties as used while creating the user.
var hacken = require('hacken');
//Do Not Provide Password While updating as passwords are encrypted while storing.
hacken.userUpdate(username,object).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Find the user by username.
var hacken = require('hacken');
hacken.userFindByUsername(username).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Find users by name.
var hacken = require('hacken');
hacken.userFindByName(name).then((result)=>{
console.log(result); //result is an array of users.
}).catch((err)=>{
console.log(err);
});
To Find user by ObjectId.
var hacken = require('hacken');
hacken.userFindById(object).then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
})
To Fetch messages of a room. //For Now roomid = "broadcast".
var hacken = require('hacken');
hacken.msgFind("broadcast").then((result)=>{
console.log(result);
}).catch((err)=>{
console.log(err);
});
To Find all the chatrooms a user is associated to.
var hacken = require('hacken');
var username = req.body.username;
var page = req.body.pagenum; //to paginate the response as there can be many chat rooms for a user.
var length = req.body.numrooms; //to specify the number of chatrooms per page.
hacken.roomFind(username,page,length).then((result)=>{
console.log(result); //Array of roomIds.
}).catch((err)=>{
console.log(err);
});
To Create a dockerimage for your project, Dockerimage will be created in your root directory.
var hacken = require('hacken');
var port = 3000; //port on which your node server will run.
var servfile = app.js; //your start file.
hacken.dockerImgCreate(port,servfile,(err)=>{
if(err){
console.log(err);
}
else{
console.log("Docker File Created.");
}
})
For Now hacken only supports group chat.
To Create socket server, call the below function in your start file.
var hacken = require('hacken');
var app = require('express')();
var http = require('http');
var server = http.Server(app);
hacken.socketFunction(server);
server.listen(3000);