and Node.js LTS 16
JaxNode October 2021
fek.io/blog
youtube.com/c/polyglotengineer
github.com/davidfekke
@jaxnode @polyglotengine1
import * as readline from 'node:readline/promises';
import { stdin as inp, stdout as outp } from 'process';
const rdln = readline.createInterface({ inp, outp });
const answer = await rdln.question('What do you think of JaxNode? ');
console.log(`Thank you for your valuable feedback: ${answer}`);
rdln.close();
Readline Promise API
import { setTimeout } from 'timers/promises';
async function doSomething() {
console.log('doSomething started!');
await setTimeout(2000);
console.log('Delayed Timers!');
}
doSomething();
import { webcrypto } from 'crypto';
const { subtle } = webcrypto;
(async function() {
const key = await subtle.generateKey({
name: 'HMAC',
hash: 'SHA-256',
length: 256
}, true, ['sign', 'verify']);
const digest = await subtle.sign({
name: 'HMAC'
}, key, 'I love node.js');
console.log(digest);
})();
const abortC = new AbortController();
abortC.signal.addEventListener('abort', () => {
console.log('Just cancelled')
}, { once: true });
abortC.abort();
console.log(abortC.signal.aborted);
const str = 'Hello JavaScript Developer!';
const strBuf = Buffer.from(str, 'utf8');
console.log(strBuf);
// <Buffer 1d e9 65 a0 96 af 69 27 2b 8a 9b 43 7a f7 a5 a2 97 ab>
const base64Buf = Buffer.from(strBuf, 'base64');
const base64Str = base64Buf.toString('base64');
console.log(base64Str);
// SGVsbG8gSmF2YVNjcmlwdCBEZXZlbG9wZXIh
const bufFromBase64Str = Buffer.from(base64Str, 'base64');
const decodedStr = bufFromBase64Str.toString('utf-8');
console.log(decodedStr);
// Hello JavaScript Developer!
console.log(str === decodedStr);
// true
{
"name": "sampleproject",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"packageManager": "yarn@1.22.11",
"dependencies": {
"axios": "^0.21.4"
}
}