A quick guide on how to setup Node.js development environment.
Previous versions of these install instructions had been tested with:
A quick guide on how to setup Node.js development environment.
Previous versions of these install instructions had been tested with:
| const echoPostRequest = { | |
| url: 'https://<my url>.auth0.com/oauth/token', | |
| method: 'POST', | |
| header: 'Content-Type:application/json', | |
| body: { | |
| mode: 'application/json', | |
| raw: JSON.stringify( | |
| { | |
| client_id:'<your client ID>', | |
| client_secret:'<your client secret>', |
| const waitFor = (ms) => new Promise(r => setTimeout(r, ms)) | |
| const asyncForEach = (array, callback) => { | |
| for (let index = 0; index < array.length; index++) { | |
| await callback(array[index], index, array) | |
| } | |
| } | |
| const start = async () => { | |
| await asyncForEach([1, 2, 3], async (num) => { | |
| await waitFor(50) |
As of just writing this, Express 4.0 was released and there are points in there that no longer matter. So, this remains as a great >4.0 article.
Node.js is the red-hot new hotness! You can't throw a stick on the internet without hitting someone talking about Node. But why? For one, it's built on JavaScript which is completely ubiquitous. So, why not build a development stack and server on JavaScript? I would argue that the installation is almost painless while the terseness of the language is not.
While you can create apps 100% from Node.js, the Express framework is a great tool that helps you solve many standard problems without having to write boilerplate code.
Node.js is here and it's not going anywhere anytime soon. So if you are new to Node.js, Express, and even JavaScript in general, this is a great newb's step-by-step guide to get started.
| # Install Pre-requisites | |
| apt update | |
| apt install apt-transport-https ca-certificates curl software-properties-common -y | |
| curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - | |
| add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | |
| apt update | |
| # Install Docker CE | |
| apt install docker-ce -y |
When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:
const Article = require('../../../../app/models/article');Those suck for maintenance and they're ugly.
| const crypto = require("crypto"); | |
| const start = Date.now(); | |
| function logHashTime(label) { | |
| crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { | |
| console.log("Hash-"+label+": ", Date.now() - start); | |
| }); | |
| } | |
| logHashTime(1); | |
| logHashTime(2); |
| const https = require('https'); | |
| const crypto = require('crypto'); | |
| const fs = require('fs'); | |
| const start = Date.now(); | |
| setImmediate(() => console.log('this is set immediate 1')); | |
| setImmediate(() => console.log('this is set immediate 2')); | |
| setImmediate(() => console.log('this is set immediate 3')); |
| var fs = require('fs'); | |
| const cache = {}; | |
| function readFile(fileName, callback) { | |
| if (cache[fileName]) { | |
| return callback(null, cache[fileName]) | |
| } | |
| fs.readFile(fileName, (err, fileContent) => { | |
| if (err) { | |
| console.log(err); |