#!/usr/bin/env node const repl = require('repl'); const crypto = require('crypto'); const replOptions = { prompt: '> ', useColors: true }; const replServer = repl.start(replOptions); const getSignatureKey = ({ key, secret, environment, }) => { const kKey = crypto.createHmac('sha256', key).digest(); const kSecret = crypto.createHmac('sha256', kKey).update(secret).digest(); const kSigning = crypto.createHmac('sha256', kSecret).update(environment).digest(); return kSigning; }; const generateSignature = ({ key = '', secret = '', environment = '' }) => { const signingKey = getSignatureKey({ key, secret, environment, }); return crypto.createHmac('sha256', signingKey).digest('hex'); }; function initializeContext (context) { context.key = null; context.secret = null; context.environment = 'dev'; } initializeContext(replServer.context); replServer.on('exit', () => { console.log('\x1b[33m%s\x1b[0m', 'Received "exit" event from repl!'); process.exit(); }); replServer.defineCommand('reset', { help: 'Reset repl', action() { initializeContext(this.context); this.displayPrompt(); }, }); replServer.defineCommand('secret', { help: 'Type "secret" to set SECRET', async action(value) { this.context.secret = value; this.displayPrompt(); }, }); replServer.defineCommand('key', { help: 'Type "key" to set KEY', async action(value) { this.context.key = value; this.displayPrompt() }, }); replServer.defineCommand('env', { help: 'Type "env" to set ENVIRONMENT, default "dev"', async action(value) { this.context.environment = value; this.displayPrompt(); }, }); replServer.defineCommand('generate', { help: 'Type "generate" to generate signature by given values', action() { if (!this.context.secret || !this.context.key) { console.log('\x1b[41m%s\x1b[0m', 'key or secret was not set'); } else { const signature = generateSignature(this.context); console.log('Signature generated: ', signature); } this.displayPrompt(); }, }); replServer.defineCommand('help', { help: 'HELP', action() { console.log('Type .secret to set SECRET. EXAMPLE: .secret SECRET'); console.log('Type .key to set KEY. EXAMPLE: .key KEY'); console.log('Type .env to set ENVIRONMENT. EXAMPLE: .env ENVIRONMENT'); console.log('Type .generate to get signature. EXAMPLE: .generate'); console.log('Type .reset to reset all values and start again. EXAMPLE: .reset'); console.log('Type .exit to close prompt. EXAMPLE: .exit'); this.displayPrompt(); }, });
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.