Skip to content

Instantly share code, notes, and snippets.

@constantology
constantology / process.logger.js
Created July 24, 2014 09:39
using node's process to emit events to log stuff from anywhere
// use like this:
// process.emit( 'app:log', module, arg1, arg2, ..., argN );
var Module = require('module');
function logConsole(method, module) {
var args = [(new Date()).toJSON(), method];
var index = 1;
if (module instanceof Module) {
@naholyr
naholyr / examples-and-issues.js
Created January 22, 2012 18:08
Writing a "url_for" helper for Express
// What name can I refer to ?
app.get('/hello-world', function () { console.log(3); });
// OK, suppose I can build the path from params
app.get('/:id/:name', function hello1 () { console.log(1); });
// OK, suppose the "callback" is the last one
app.get('/hello/:name', requiresAuthentication, function hello2 () { console.log(2); });
// How do I build the path ?
@youurayy
youurayy / filesender.js
Created November 9, 2011 09:02
Node.js (v0.4) sendfile() non-caching fileserver - fast and efficient!
var http = require('http');
var fs = require('fs');
var path = require('path');
var constants = require('constants');
var timers = require('timers');
var port = 8080;
http.createServer(function(req, res) {
if(req.url == '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
@naholyr
naholyr / Makefile
Last active March 6, 2018 17:02
browserify-friendly Makefile
# Main entry point
SOURCE = src/app.js
# The dependencies (other than node_modules/**)
LIBS = $(shell ls src/lib/*.js src/components/*.js)
# The target
TARGET = build/app.js
# Compilation flags
@nhoizey
nhoizey / index.html
Created September 13, 2011 12:47
Un affichage de numéro vert avec Microformat et mise en forme en CSS
<!-- Live demo : http://bl.ocks.org/1213727 -->
<html>
<head>
<title>Un affichage de numéro vert avec Microformat et mise en forme en CSS</title>
<link rel="stylesheet" media="all" href="numero_vert.css" type="text/css" />
</head>
<body>
<span class="vcard numerovert">
<span class="fn">Nom de la société</span>
@tj
tj / routes.js
Created October 15, 2011 00:23
Express routes
var app = require('../app');
console.log();
app.routes.all().forEach(function(route){
console.log(' \033[90m%s \033[36m%s\033[0m', route.method.toUpperCase(), route.path);
});
console.log();
process.exit();
@naholyr
naholyr / 01-db-test.js
Created August 27, 2011 09:39
REST avec NodeJS & Express - Tests Unitaires
var assert = require('assert')
, db = require('../db')({namespace:'bookmarks-test-db'})
, the_bookmark = {}
require('vows')
.describe('bookmarks-db')
.addBatch({
"Initialize": {
topic: function () {
db.deleteAll(this.callback);
@naholyr
naholyr / app.js
Created August 27, 2011 10:13
REST avec NodeJS & Express - Application
var express = require('express')
, app = module.exports = express.createServer()
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function () {
@tj
tj / equivalent.js
Created August 24, 2011 00:21
example of backbone-style routing with Express
app.get('/help', function(req, res){
res.send('some help');
});
app.get('/search/:query/p:page', function(req, res){
var query = req.params.query
, page = req.params.page;
res.send('search "' + query + '", page ' + (page || 1));
});
@n1k0
n1k0 / meta-extract.js
Created January 21, 2012 07:02
CasperJS script to extract meta informations
var casper = require("casper").create()
, url = casper.cli.get(0)
, metas = [];
if (!url) {
casper.echo('Usage: casperjs [url]').exit();
}
casper.on('remote.message', function(msg) {
console.log(msg);