Skip to content

Instantly share code, notes, and snippets.

View lennym's full-sized avatar

Leonard Martin lennym

View GitHub Profile
@lennym
lennym / index.js
Created April 24, 2015 11:26
console.log calls inspect
var app = {
inspect: function () {
return 'a kitten!';
}
};
console.log(app);
@lennym
lennym / routers.js
Last active August 29, 2015 14:20
Nested routers
var express = require('express');
var app = express();
var jobs = express.Router();
var properties = express.Router();
var property = express.Router();
jobs.route('/:id')
.get(function (req, res, next) {
@lennym
lennym / gist:e1b063d40e94027b84fb
Last active August 29, 2015 14:21
url normalisation middleware
var url = require('url');
app.use(function (req, res, next) {
if (req.path.indexOf('&') > -1) {
var path = req.path.split('&')[0];
res.redirect(url.format({
pathname: path,
query: req.query
}));
} else {
@lennym
lennym / sass
Created June 3, 2015 10:27
node-sass build script
#!/usr/bin/env node
var sass = require('node-sass'),
path = require('path'),
fs = require('fs');
var src = path.resolve(__dirname, '../assets/sass/app.scss'),
out = path.resolve(__dirname, '../public/stylesheets/app.css');
sass.render({
11:22 AM <lennym> Morning all, I'm having some issues with node-sass and npm, but only in certain contexts. I was hoping to talk it through to see if I'm doing anything obviously stupid.
11:23 AM <lennym> I have a project, which has a module as a dev dependency - both depend on node sass, and both run node sass as part of a postinstall script
11:24 AM <lennym> Except the postinstall script fails in node-sass for the child module with an error of "Cannot find module semver" here - https://github.com/sass/node-sass/blob/v2.1.1/lib/extensions.js#L1
11:24 AM <lennym> When run not as a child dependency of the parent project it works fine.
11:24 AM <lennym> Both projects have the same (2.1.1) dependency of node-sass
11:25 AM <lennym> Now here's the kicker... it *only* fails as part of an npm install on our jenkins build server, I can't replicate locally.
11:26 AM <lennym> If anyone has any ideas or suggestions I'd be happy to hear them.
function requestHandler(req, res) {
fs.readFile('/my/file', function (err, file) {
if (err) {
res.status(500).send();
} else {
res.send(file);
}
});
}
var app = require('express')();
app.get('/foo', function (req, res, next) {
res.send(req.path);
// here req.path === '/foo'
});
app.use('/bar', function (req, res, next) {
res.send(req.path);
// here req.path === '/'
@lennym
lennym / gist:703272
Created November 17, 2010 11:09 — forked from avhm/gist:703244
$.expr[':'].onscreen = function(obj){
var $win = $(window), pos = $(obj).position();
return $.contains(document.documentElement, obj) && pos.top < $win.height() && pos.left < $win.width();
};
$.expr[':'].offscreen = function(obj){
return !$(obj).is(':onscreen');
};
@lennym
lennym / gist:1164645
Created August 23, 2011 08:27
Object passing by reference in Backbone.Model default values.
var TestModel = Backbone.Model.extend({
defaults: {
primitiveProp: 'foo',
objectProp: {
prop1: 'val',
prop2: null
}
}
});
@lennym
lennym / gist:1270328
Created October 7, 2011 13:50
Odd arguments behaviour in Google Chrome
function foo (val) {
console.log(arguments, arguments[0]);
val = 2;
}
foo(1);
// logs "[2], 1" in Google Chrome