Mahfuz JS is a Node.js MERN Stack library that provides various utilities including server setup, database connection, encryption, email sending, and GitHub release checking.
To install the module, use npm:
npm install mahfuz-js
First, import the required classes from the module:
import { Backend, DatabaseConnector, Utils, ApiResponse, ApiError, MahfuzMailer, UpdateChecker } from 'mahfuz-js';
const backend = new Backend();
backend.startServer();
const dbConfig = {
Host: 'localhost',
Port: 27017,
Username: 'your-username',
Password: 'your-password',
DbName: 'your-db-name'
};
const dbConnector = new DatabaseConnector(dbConfig);
dbConnector.ConnectToMongoDB();
const dbConfig = {
SubDomain: 'your-subdomain',
Username: 'your-username',
Password: 'your-password',
Cluster: 'your-cluster-name'
};
const dbConnector = new DatabaseConnector(dbConfig);
dbConnector.ConnectToMongoDBAtlas();
The Utils
class provides utility functions for generating random numbers, handling async requests, and encrypting data.
const utils = new Utils();
const randomNumber = utils.GenerateRandomNumber(10);
const encryptedString = utils.Encrypt('your-string');
const isMatch = utils.Compare('your-string', encryptedString);
The ApiResponse
class is used to standardize API responses.
const response = new ApiResponse(200, { key: 'value' }, 'Success message');
The ApiError
class is used to standardize API error responses.
const error = new ApiError(400, 'Error message', ['error1', 'error2']);
The MahfuzMailer
class is used to send emails using nodemailer.
const mailConfig = {
Name: 'Your Name', \\ Your name or company name
From: 'your-email@example.com', \\ Your email address that you used to register for the SMTP server
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
};
const mailer = new MahfuzMailer(mailConfig);
mailer.SendMail('recipient@example.com', 'Subject', '<h1>Email Body</h1>');
The UpdateChecker
class is used to check for the latest release of a GitHub repository and retrieve the download URL for a specific operating system.
const updateConfig = {
GithubUsername: 'your-github-username',
RepoName: 'your-repo-name',
RepoToken: 'your-github-token' \\ Your GitHub personal access token note: please store your tokens securely inside the dot env file
};
const checker = new UpdateChecker(updateConfig);
checker.CheckLatestVersion('windows')
.then(result => {
if (result.success) {
console.log('Download URL:', result.downloadUrl);
} else {
console.log('Error:', result.message);
}
})
.catch(error => {
console.error('Error:', error);
});
The CheckLatestVersion
method supports the following operating systems:
-
windows
(returns.exe
files) -
mac
(returns.dmg
files) -
linux
(returns.deb
files)
The CheckLatestVersion
method returns an object with a success
property indicating whether the operation was successful. If success
is false
, the message
property contains an error message.
This project is licensed under the MIT License.
Mahfuz JS is developed by Mohammad Mahfuz Rahman.
Happy Coding!