Skip to content

Instantly share code, notes, and snippets.

@joepie91
joepie91 / middleware.js
Created January 12, 2015 23:41
Using a persistent object in Express
someObject = instantiateSomePersistentObjectSomehow();
app.use(function(req, res, next) {
req.someObject = someObject;
next();
});
// ...
app.get("/sample/route", function(req, res) {
@joepie91
joepie91 / asynchronous.js
Last active November 29, 2021 11:16
PHP vs Node.js: Synchronous vs Asynchronous
console.log("Before the first file is read.");
hypotheticalFileGetContents("sample.txt", function(fileContents){
// fileContents now contains the file contents, this function is only called when the file read in the background has finished
console.log("After the first file has completed reading.");
});
// You've now told it to start the first read, but it won't 'block' your script execution. It will do the read in the background, and immediately move on with the rest of your code.
console.log("Before the second file is read.");
hypotheticalFileGetContents("sample2.txt", function(fileContents){
@joepie91
joepie91 / closures.js
Last active October 14, 2018 05:54
Javascript Closures illustrated
functionCreator = function(favouriteNumber) {
return function(inputNumber) { // <-- This function is a closure!
console.log("You input the number " + inputNumber + ", and your favourite number is " + favouriteNumber + ".");
if(inputNumber === favouriteNumber) {
console.log("Those are the same number!");
} else {
console.log("Those are NOT the same number!");
}
}
exports.convertXLSXToJSON2 = function(req, res) {
var path = fileServerPath + req.body.name;
console.log("starting..." + path);
var exists = fs.existsSync(path);
console.log("path exists? " + exists);
console.log("in xlsx to json 2 method...starting conversion");
try {
var obj = asyncXLSX.parse(path);
console.log("obj: " + obj);
res.send(obj);
@joepie91
joepie91 / gulpfile.js
Created February 9, 2015 04:12
NW.js (node-webkit) Gulp pipeline with auto-reload
/* Based on https://gist.github.com/spelufo/10872517 */
var gulp = require('gulp');
var child = require('child_process');
var childProcesses = {};
var jade = require("gulp-jade");
/* CoffeeScript compile deps */
var gutil = require('gulp-util');

In response to http://www.jongleberry.com/why-i-hate-npm.html. Some valid points raised, but lost in a sea of nonsense.

Downtime and errors

  • Haven't experienced this. At all.

Slow

  • npm is the fastest package manager I've ever personally used. I'm not aware of any consistently faster ones.
  • How long npm install takes for an already installed application is not a relevant metric - that's not a real-world scenario.
  • Component, by its own admission, downloads selectively. Of course it will be faster. Does he have any explanation as to why Component is faster?
@joepie91
joepie91 / express-global.js
Created February 17, 2015 16:48
Making things available globally in Express
db = getDatabaseConnectionSomehow();
app.use(function(req, res, next){
req.db = db;
next();
});
// [...]
app.get("/", function(req, res) {
@joepie91
joepie91 / some-module.js
Last active August 29, 2015 14:15
Configurable exports in Node.js
module.exports = function(configuration) {
var configuredInstance = createInstance(configuration);
return function(argOne, argTwo) {
configuredInstance.doThing(argOne)
return configuredInstance.doAnotherThing(argTwo);
}
}
@joepie91
joepie91 / filter.js
Created March 2, 2015 20:29
Array filter method in Javascript
listOfNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
newList = listOfNumbers.filter(function(number) {
return (number <= 5);
});
console.log(newList); // [ 0, 1, 2, 3, 4, 5 ]
@joepie91
joepie91 / new-promises-fallback.js
Last active August 2, 2016 21:04
Fallback values in promise chains
/* UPDATED: This example has been changed to use the new object predicates, that were
* introduced in Bluebird 3.0. If you are using Bluebird 2.x, you will need to use the
* older example below, with the predicate function. */
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs"));
Promise.try(function(){
return fs.readFileAsync("./config.json").then(JSON.parse);
}).catch({code: "ENOENT"}, function(err){