Skip to content

Instantly share code, notes, and snippets.

View AliSawari's full-sized avatar
🌩️
Brainstorming...

Ali Sawari AliSawari

🌩️
Brainstorming...
View GitHub Profile
@AliSawari
AliSawari / body-parser.js
Last active January 17, 2024 15:42
Simple Body Parser in Node.js
const { createServer } = require('http');
const server = createServer((request, response) => {
// by default assign maximum of 2KB for Body size if there's no Content-Length
let size = Number(request.headers['content-length']) || 2048;
let bodyData = Buffer.alloc(size);
request.on('data', data => {
for (let x = 0; x < size; x++) {
bodyData[x] = data[x];
@AliSawari
AliSawari / rxjs.array-over-time-iteration.js
Created October 13, 2020 22:47 — forked from allenevans/rxjs.array-over-time-iteration.js
RxJS iterate over an array by time
const keyInputs = [
['n', 1],
['e', 10],
['w', 100],
[' ', 1],
['y', 10],
['o', 100],
['r', 300],
['k', 200]
];
{"lastUpload":"2020-05-27T15:30:36.153Z","extensionVersion":"v3.4.3"}
const { readFile } = require('fs')
const { promisify } = require('util')
const readFilePromise = promisify(readFile)
readFilePromise('myFile').then(data => {
console.log(data.toString())
}).catch(e => console.log(e))
// import it!
const { promisify } = require("util")
// turn your ordinary func into a promise func!
const doublePromise = promisify(double)
// use it!
doublePromise(14).then(res => {
console.log(res)
}).catch(err => console.log(err))
function double(number, callback){
if(number > 2){
callback(null, number * 2)
} else {
callback(new Error("number should be more than 2"))
}
}
@AliSawari
AliSawari / promise.js
Created June 4, 2019 13:41
here's how to create promise-based functions
// say take a number and return its double
// resolve : the success call . which goes in the first then
// reject : the error call, which goes in .catch
function double(number){
return new Promise((resolve, reject) => {
if(typeof number == 'number'){
resolve(number * 2)
} else reject("the argument should a Number")
@AliSawari
AliSawari / then.js
Created June 4, 2019 13:31
the arguments in then calls are the returns of the previous ones
// with each return , the value goes for the next then in chain
// you can chain as many thens as you want, even if you dont return a value
doSomething(args).then(result => {
return result + 2
}).then(plusTwo => {
return plusTwo + 2
}).then(plusFour => {
// and the chain continues as many thens as you want...
}).catch(err => console.log(err))
@AliSawari
AliSawari / callback.js
Last active June 4, 2019 13:26
a good way to get rid of the callbacks in nodejs
// ok
@AliSawari
AliSawari / index.js
Created March 17, 2019 12:06 — forked from akexorcist/index.js
Axios post method requesting with x-www-form-urlencoded content type
const axios = require('axios')
...
const requestBody = {
name: 'Akexorcist',
age: '28',
position: 'Android Developer',
description: 'birthdate=25-12-1989&favourite=coding%20coding%20and%20coding&company=Nextzy%20Technologies&website=http://www.akexorcist.com/',
awesome: true