Skip to content

Instantly share code, notes, and snippets.

View cjroth's full-sized avatar
😃
build mode

Chris Roth cjroth

😃
build mode
View GitHub Profile
@cjroth
cjroth / require-regex.md
Created February 23, 2014 22:54
convert old style imports to new style with regex

converts old style imports:

var express            = require('express')
  , redismodule        = require('redis')
  , _                  = require('underscore')

to new style:

var express = require('express');
/*
an orm should only take care of mapping objects to a relational database. that's **all**. a lot of my problems with
sequelize come from the fact that i'm using sequelize to define my models. i would much prefer to use plain javascript
objects to define my models - not using sequelize.define or some other sort of "definer". ideally i could follow a more
classic object-oriented approach where my models could extend or implement an orm that takes care of relaying data back and
fourth between the database and client and nothing else.
*/
var ORM = function() {
this._as = {};
@cjroth
cjroth / middleware-admin.js
Last active August 29, 2015 14:05
node/express admin middleware
module.exports = function() {
return function(req, res, next) {
if (!req.isAuthenticated()) {
return res.status(401).end();
}
if (req.user.role !== 'admin') {
return res.redirect('/');
}
return next();
};
@cjroth
cjroth / middleware-authorize.js
Last active August 29, 2015 14:05
node/express authorization middleware
module.exports = function() {
return function(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
return res.status(401).end();
};
};
@cjroth
cjroth / middleware-errors.js
Created September 1, 2014 08:37
node/express errors middleware (wip)
var util = require('util');
var loggly = require('../services/loggly');
function middleware() {
return function(err, req, res, next) {
if (!util.isError(err)) {
var error = new Error();
if (typeof err === 'number') {
error.status = err;
@cjroth
cjroth / middleware-last-active.js
Created September 1, 2014 08:38
node/express last active middleware
module.exports = function() {
return function(req, res, next) {
if (!req.user) return next();
var sql = req.app.services.sql;
sql
.query('update users set last_active = $1 where id = $2;', [new Date(), req.user.id])
@cjroth
cjroth / email-service.js
Created September 1, 2014 08:40
node/express email service
/**
* inlines all css
* @todo should replace sendgrid with smtp to make more reusable
*/
var _ = require('underscore');
var config = require('../config');
var fs = require('fs');
var jade = require('jade');
var juice = require('juice');
@cjroth
cjroth / id.js
Created September 1, 2014 08:42
validate and parse integer ids
/**
* validates and parses ids. optionally accepts multi-ids (comma separated integer ids)
*/
var validator = require('validator');
var parse = function(ids, multiple) {
if (!ids) {
return false;
@cjroth
cjroth / geo-distance.js
Created September 1, 2014 08:43
get distance between two coordinates in kilometers
var convertDegToRad = require('./convert-deg-to-rad');
module.exports = function(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = convertDegToRad(lat2 - lat1);
var dLon = convertDegToRad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(convertDegToRad(lat1)) * Math.cos(convertDegToRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
@cjroth
cjroth / deg2rad.js
Created September 1, 2014 08:44
convert degrees to radians
module.exports = function(deg) {
return deg * (Math.PI / 180);
};