Skip to content

Instantly share code, notes, and snippets.

@branneman
branneman / better-nodejs-require-paths.md
Last active April 27, 2024 04:16
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

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.

Possible solutions

@mrded
mrded / server.js
Created December 4, 2015 16:17
CORS Proxy on node.js + express
var express = require('express'),
request = require('request');
var app = express();
// Forward all requests from /api to http://foo.com/api
app.use('/api', function(req, res) {
req.pipe(request("http://foo.com/api" + req.url)).pipe(res);
});
@dImrich
dImrich / curved3Plane.js
Created January 6, 2017 18:00
Bend Three JS Plane Geometry With Bezier Curve
//Bend Three JS plane geometry with bezier curve
//Curved plane generation, bezier curve plane,
function bendPlaneGeometry(planeGeometry, centerBendZ)
{
var curve = new THREE.CubicBezierCurve3(
planeGeometry.vertices[0],
new THREE.Vector3(planeGeometry.parameters.width/2, 0, centerBendZ ),
new THREE.Vector3(planeGeometry.parameters.width/2, 0, centerBendZ ),
planeGeometry.vertices[(planeGeometry.vertices.length/2) - 1]
);
@loilo
loilo / bubble.md
Last active April 27, 2023 00:21
Make Vue events bubble

Make Vue events bubble

Vue events don't bubble the component tree on their own. However when writing wrapper components this can be the desired behaviour.

This code registers a global bubble directive which allows to re-emit all given events:

Let's say we want to bubble events start, accelerate and brake of our component Car.

Without any help, we'd roughly have to do this:

@schleg
schleg / 01. Gemfile
Created May 26, 2011 17:26
Setup for Devise + Omniauth
gem 'pg'
group :development do
gem 'ruby-debug'
end
gem 'rake', '~> 0.8.7'
gem 'devise'
gem 'oa-oauth', :require => 'omniauth/oauth'
gem 'omniauth'
gem 'haml'
gem 'dynamic_form'
@theatlasroom
theatlasroom / basic-post-receive-hook-with-pm2
Last active August 16, 2022 23:00
Basic post-receive hook - node + pm2
#!/bin/sh
# This is for an expressjs node app, it uses npm + bower packages and pm2 to start the app
# [pm2](https://github.com/Unitech/pm2)
# Assumes you've created a [bare git repo](https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server) with on your server with git init --bare
# Adapted from http://javascript.tutorialhorizon.com/2014/08/17/push-to-deploy-a-nodejs-application-using-git-hooks/
PORT=1337
APP_NAME="app-name"
APP_ROOT="/var/www/app"
@possibilities
possibilities / meteor-async.md
Created August 23, 2012 22:53
Meteor Async Guide

From Meteor's documentation:

In Meteor, your server code runs in a single thread per request, not in the asynchronous callback style typical of Node. We find the linear execution model a better fit for the typical server code in a Meteor application.

This guide serves as a mini-tour of tools, trix and patterns that can be used to run async code in Meteor.

Basic async

Sometimes we need to run async code in Meteor.methods. For this we create a Future to block until the async code has finished. This pattern can be seen all over Meteor's own codebase:

@antishok
antishok / NODE_ENV production.md
Last active September 25, 2020 07:09
What does `NODE_ENV=production` do?
@aseemk
aseemk / randomStr.js
Created July 12, 2012 05:00
Random alphanumeric (base-62) strings in Node.js, cryptographically strong
var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
### define function variable before block to avoid code being appended to closing part of JSDoc comment ###
cube = ''
###*
* Funtion to calculate cube of input
* @param {number} Number to operate on
* @return {number} Cube of input
###
cube = (x) -> x*x*x