This file contains hidden or 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
// listen to the parent process | |
process.on('message', (message) => { | |
// send the message to the function | |
const response = calculatePrime(message.primeNumber); | |
// send the response from the function back to the parent process. | |
process.send(response); | |
// exit/shutdown the process immediately | |
process.exit(); | |
}); |
This file contains hidden or 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 childProcess = fork('./calculatePrime.mjs'); | |
childProcess.send({ primeNumber: parseInt(req.query.number) }); | |
childProcess.on('message', (message) => res.json(message)); |
This file contains hidden or 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 { fork } from 'child_process'; | |
// the file we want to execute | |
const childProcess = fork('./calculatePrime.mjs') |
This file contains hidden or 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 express from 'express'; | |
const app = express(); | |
app.get('/calculateprime', (req, res) => { | |
const response = isPrime(parseInt(req.query.number)); | |
return res.json(response); | |
}); | |
app.listen(5000, () => console.log('Listening on 8081')); |
This file contains hidden or 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
{ | |
"name": "prime-block", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", |
This file contains hidden or 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
$ npm init -y |
This file contains hidden or 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
class Blockchain { | |
constructor() { | |
this.chain = [this.createGenesisBlock()]; | |
} | |
createGenesisBlock() { | |
return new Block('17/102021', 'First Block'); | |
} | |
appendBlock(newBlock) { |
This file contains hidden or 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
class Block { | |
constructor(time, data, previousHash = '') { | |
this.time = time; | |
this.data = data; | |
this.previousHash = previousHash; | |
this.hash = this.getHash() | |
} | |
getHash() { | |
return crypto.createHash('sha256').update(this.previousHash + this.time + JSON.stringify(this.data)).digest('hex'); |
This file contains hidden or 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
remove(index) { | |
// check if index is out of size range | |
if (index < 0 || index >= this.length) { | |
return; | |
} | |
let current = this.head; | |
let previous; | |
let count = 0; |
This file contains hidden or 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
class LinkedList { | |
constructor() { | |
this.head = null; | |
this.tail = this.head; | |
this.length = 0; | |
} | |
append(data) { | |
let node = new Node(data); | |
if(!this.head) { |