This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A simple BlockChain example via Nodejs | |
// this example shows the core functionality of a BlockChain system and how it works | |
// want to add more options? here's the GitHub rep: https://github.com/AliSawari/SimpleBlockChain | |
// THIS EXAMPLE IS INSPIRED BY : youtu.be/zVqczFZr124 | |
// requiring Hash function | |
const {createHmac} = require('crypto'); | |
// simplify some functions | |
function str(val){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var fs = require("fs"); | |
var server = http.createServer().listen(3000); | |
server.on("request", function(req, res) { | |
if (req.method != "POST") return res.end(); | |
var imageName = "received-" + Date.now() + ".jpg"; | |
var writeStream = fs.createWriteStream(imageName); | |
req.pipe(writeStream); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// including the net module | |
const net = require('net'); | |
// clients array | |
let clients = []; | |
// a nice little logger with a time stamp | |
// loggin messages for both server and Client | |
function logger(m){ | |
let d = new Date().toLocaleString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function double(number, callback){ | |
if(number > 2){ | |
callback(null, number * 2) | |
} else { | |
callback(new Error("number should be more than 2")) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
OlderNewer