Skip to content

Instantly share code, notes, and snippets.

$sprites: sprite-map("sprites/*.png");
$sprites-retina: sprite-map("sprites-retina/*.png");
@mixin sprite-background($name) {
background-image: sprite-url($sprites);
background-position: sprite-position($sprites, $name);
background-repeat: no-repeat;
display: block;
height: image-height(sprite-file($sprites, $name));
width: image-width(sprite-file($sprites, $name));
@getchenge
getchenge / express.js
Last active August 29, 2015 14:02
express config
'use strict';
var express = require('express'),
favicon = require('static-favicon'),
logger = require('morgan'),//loging middleware
compression = require('compression'),
bodyParser = require('body-parser'),//urlencoded json
methodOverride = require('method-override'),//enable http verbs on client
cookieParser = require('cookie-parser'),
session = require('express-session'),
@getchenge
getchenge / server.js
Created June 23, 2014 14:56
the entrance
'use strict';
var express = require('express');
/**
* Main application file
*/
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
app.get('/user/:id', function(req, res){
res.send('user ' + req.params.id);
});
"/user/:id"
/user/12
"/users/:id?"
/users/5
/users
@getchenge
getchenge / params.js
Last active August 29, 2015 14:02
express.param
/**
* Module dependencies.
*/
var express = require('../../');
var app = module.exports = express();
// Faux database
var users = [
@getchenge
getchenge / static.js
Created June 29, 2014 14:10
中间件例子_静态文件夹
var path = require('path');
var fs = require('fs');
var url = require('url');
var app = require('express')();
var staticfile = function (req,res,next) {
var pathname = url.parse(req.url).pathname;
fs.readFile(path.join(ROOT, pathname), function (err, file){
if(err){
return next();
@getchenge
getchenge / data.js
Created July 3, 2014 04:54
通过api获取数据
'use strict';
var md5 = require('MD5'),
_ = require('lodash'),
async = require('async'),
https = require('http'),
request = require('request'),
app_id = '100',
keys = 'testkeys123',
api_host = 'http://test.5wei.com/';
@getchenge
getchenge / directive.js
Last active August 29, 2015 14:04
ng-directives
angular.module('directivlor', [
'ngCookies',
'ngResource',
'ngSanitize',
'ui.router'
])
//contenteditable data two-way bind
.directive('contenteditable', ['$sce', function ($sce) {
return {
Highcharts.Chart.prototype.callbacks.push(function(chart) {
var hasTouch = hasTouch = document.documentElement.ontouchstart !== undefined,
mouseTracker = chart.tracker,
container = chart.container,
mouseMove;
mouseMove = function (e) {
// let the system handle multitouch operations like two finger scroll
// and pinching
if (e && e.touches && e.touches.length > 1) {

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:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions