MKRWEB
Hello 👋 ! My name is Sarkis! I'm a beginner web developer. And I would like to share with you my small package for server-side development.
So what's included in this package?
- Easier work with the mysql database Using the mysql2 package
- Methods for encrypting/decrypting data, as well as clearing strings from HTML
- A method that will allow you to block users (bots) who make a lot of requests to the server
SQL
Create connection
const mkrweb = new MKRWEB({
sql: {
database: 'mkrweb_db',
host: 'localhost',
username: 'root',
password: ''
}
})
Assign the path and language for recording log files
const sql = mkrweb.sql();
sql.setLanguage("EN");
sql.setPath(join('sql.log'));
Exmaple log file
-------ERROR IN ASYNCHRONOUS REQUEST-------
Code: ER_PARSE_ERROR
Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'use' at line 1
Request: SELECT * FROM use
Time: 23.11.2023 22:22:49
Select data from database (Synchronous / Asynchronous)
const data = {
columns: '*',
identifier: "name = 'Alex'",
order: {
column: 'id',
type: "DESC",
limit: 10
}
}
sql.select<userType[]>('user', data, result => console.log(result));
sql.selectSync<userType[]>('users', data).then(result => console.log(result));
Add data to database (Synchronous / Asynchronous)
const data = [null, 'Alex', 18, 'alex@email.com', 'hwabcdksjb'];
sql.add('users', data, added => {
if(added) console.log('user added');
else console.log('user not added');
});
sql.addSync('users', data).then(added => {
if(added) console.log('user added');
else console.log('user not added');
})
Update data in database (Synchronous / Asynchronous)
const columns = ['name', 'email'];
const newData = ['Jack', 'jack@email.com'];
sql.update('users', {columns: columns, values: newData, identifier: 'id = 5'}, updated => {
if(updated) console.log('user updated');
else console.log('user not updated');
})
sql.updateSync('users', {columns: columns, values: newData, identifier: 'id = 5'}).then( updated => {
if(updated) console.log('user updated');
else console.log('user not updated');
})
Delete data from database (Synchronous / Asynchronous)
sql.delete({table: 'users', identifier: 'id = 5'}, deleted => {
if(deleted) console.log('user deleted');
else console.log('user not deleted');
});
sql.deleteSync({table: 'users', identifier: 'id = 5'}).then(deleted => {
if(deleted) console.log('user deleted');
else console.log('user not deleted');
});
CRYPTER
Get a crypter
const crypto = mkrweb.crypt();
Encrypt data
You can encrypt both strings and objects, arrays, etc...
const string = 'hello world!';
const user = {
name: 'Alex',
age: 18
}
const encryptString = crypto.encrypt(string);
const encryptUser = crypto.encrypt(JSON.stringify(user));
Decrypt data
const decryptString = crypto.decrypt(encryptString);
const decryptUser = crypto.decrypt(encryptUser);
Remove HTML tags
const user = {
name: '<h1>Alex</h1>',
age: '<script>alert(18)</script>'
}
const removeHtml = crypto.clearHTML(user);
BotControll
This method monitors the number of requests to your server and if the requests exceed the norm, it blocks the user’s ip
Create connection
const mkrweb = new MKRWEB({
bot: {
path: join(__dirname, 'bots.JSON'),
block: join(__dirname, 'block.JSON'),
interval: 3000,
maxRequests: 50
}
})
How does it work
const ip = '15.210.48.54';
for(let i = 0; i < 100; i++){
if(bot.check(ip)) {
console.log('user blocked!'); break;
}
}