mahfuz-js
v1.4.3
Published
Mern Stack Development Tool
Readme
Mahfuz JS Backend Library
Overview
Mahfuz JS is a Backend library that provides various utilities including server setup, database connection, encryption, email sending, and GitHub release checking for backend development.
Installation
install using npm:
npm install mahfuz-jsUsage
Setup Backend Server
inside index.js
import 'dotenv/config';
import { Backend } from 'mahfuz-js';
// Initialize Backend
const FrontendURL = process.env.FRONTEND_URL;
const dbConfig = {
subdomain: process.env.DB_SUBDOMAIN,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
cluster: process.env.DB_CLUSTER
};
const dbConnected = await Backend.ConnectToDataBase(dbConfig);
if(dbConnected) {
const frontendConnected = await Backend.ConnectToFrontend(FrontendURL);
if(frontendConnected) {
Backend.ConfigureMiddlewares(true);
Backend.AddSecurityHeaders(true);
await Backend.StartServer(process.env.BACKEND_PORT)
}
} else {
console.log("database connection failed");
}
Utils Class
The Utils class provides utility functions for generating random numbers, handling async requests, and encrypting data.
import { Utils } from "mahfuz-js";
const Random = Utils.Random(10);
const encryptedString = Utils.Encrypt('your-string');
const isMatch = Utils.Compare('your-string', encryptedString);ApiResponse
The ApiResponse class is used to standardize API responses.
const response = new ApiResponse(200, { key: 'value' }, 'Success message');ApiError
The ApiError class is used to standardize API error responses.
const error = new ApiError(400, 'Error message', ['error1', 'error2']);MahfuzMailer
The MahfuzMailer class is used to send emails using nodemailer.
const mailConfig = {
Host: 'smtp.example.com', \\ Your SMTP server host address
Port: 465, \\ Your SMTP server port
Username: 'your-username', \\ Your SMTP server username that SMTP server provided
Password: 'your-password' \\ Your SMTP server password that SMTP server provided
Name: 'Your Name', \\ Your name or company name
From: '[email protected]', \\ Your email address that you used to register for the SMTP server
To: '[email protected]',
Subject: 'Subject',
Body: 'html-content',
};
const isSent = await MahfuzMailer.SendMail(mailConfig);
if(isSent) {
console.log("mail sent");
} else {
console.log("mail could not send");
}License
This project is licensed under the MIT License.
Author
Developed by Mohammad Mahfuz Rahman.
Happy Coding!
