beatific
v1.2.2
Published
No need to install mongoose, bcrypt, jsonwebtoken, helmet, compression, morgan. This library does every functionality of them for you. And it can generate dockerfile for project as well.
Downloads
10
Maintainers
Readme
Beatific
A single npm package which satisfies the need for the following packages:
- mongoose => For establishing connection to your MongoDB and producing mongoose models.
- bcryptjs => For hashing and verifying hashes.
- helmet => For securing Express apps by setting various http headers.
- compression => For the compression of route propagation.
- jsonwebtoken => For generating and verifying JWTs.
- morgan => For logging every request made to your backend.
It also generates the dockerfile for your project :)
Usage:
Installation:
npm install --save beatific
API:
var beatific = require("beatific");
MongoDB/Mongoose Functionality:
- Connect to a MongoDB
Parameters: MongoDB URI
beatific.mongoConnect(dbURI)
.then(db => console.log("Connected to the DB..."))
.catch(err => console.error("Error connecting to the DB!"));
- Create a mongo schema and automatically generate a mongoose.model for it
Parameters: Schema name, schema object, db collection name (optional)
let userSchema = {
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
}
};
beatific.mongoModelGen('User', userSchema, "users")
.then(model => console.log("mongoose.model generated for user schema"))
.catch(err => console.error("Some problem occurred"));
What this does behind the scenes
const mongoose = require("mongoose");
let userSchema = {
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
}
};
let schema = new mongoose.schema(userSchema);
return mongoose.model('User', schema, "users");
JWT functionality:
- Signing/Generating the token:
Parameters: data, secret, expiresIn (optional)
beatific.generateJWT({message: "Hey there"}, 'something_secret', '4d')
.then(token => console.log("Here's the token " + token))
.catch(err => console.error("Some problem occurred"));
- Verifying/Decoding the token:
Parameters: token, secret
beatific.decodeJWT('my_token_1234321', 'something_secret')
.then(decoded => console.log("Here's the decoded token " + decoded))
.catch(err => console.error("Some problem occurred"));
Bcrypt Functionality:
- Hashing some data:
Parameters: data, salt rounds (default=10)
beatific.hashGen("hey there", 8)
.then(hash => console.log("Here's the hash " + hash))
.catch(err => console.error("Some problem occurred"));
- Verifying/Comparing some data and corresponding hash:
Parameters: hashed data, inputData
beatific.hashCheck("$adfdsf23243546524", "my password may be")
.then(valid => console.log("Status of check: " + valid))
.catch(err => console.error("Some problem occurred"));
Use logger
Parameters: loggerType (default = "dev")
//In your main .js file
var app = express();
loggerType = "short"; // For example
app.use(beatific.logger(loggerType));
Use helmet:
// In your main .js file
var app = express();
app.use(beatific.helmet());
Use compression:
// In your main .js file
var app = express();
app.use(beatific.compression());
Generate Dockerfile for the project
Parameters: dockerfile_name (default="dockerfile"), portNumber, startCommand (default="npm start")
Just run this once in your main .js file:
beatific.dockerGen("dockerfile.dev", 8000, "npm test");