Skip to content

Instantly share code, notes, and snippets.

@nanha
nanha / app.js
Last active December 16, 2015 21:48
nodeqa.com 답변 nodejs 기본 route 방법
var http = require('http'), path = require('path'), fs = require('fs');
http.createServer(function(req, res) {
var file = path.normalize('.' + req.url);
path.exists(file, function(a) {
if (a === false) {
res.writeHead(404);
res.end('Not found');
return;
}
@nanha
nanha / nodejs-v0.10-duplex-stream-pipe.js
Created March 13, 2013 04:08
node.js v0.10. StreamV2. duplex stream
var stream = require('stream');
var dest = new stream.Duplex, dest2 = new stream.Duplex;
dest._read = function(size) {};
dest._write = function(chunk, encoding, cb) {
this.push(chunk.toString().toUpperCase());
cb();
};
dest2._read = function(size) {};
@nanha
nanha / nodejs-v0.10-duplex-stream-split.js
Created March 13, 2013 03:56
Awesome module split (https://github.com/dominictarr/split/blob/master/index.js) Customize for Nodes.js v0.10 Use StreamV2, duplex
/**
* https://github.com/dominictarr/split
* Customize for Node.js v0.10
*
* - remove through module
https://github.com/dominictarr/through/blob/master/index.js
* - use stream.Duplex (StreamV2)
*
* @author nanhapark
* @twitter @nanhapark
@nanha
nanha / gist:5126502
Created March 10, 2013 00:29
domain error handler + bind function
function User() {
this.name = 'nanha';
}
var Nanha = new User();
var fs = require('fs');
var domain = require('domain').create();
domain.on('error', function(err) {
console.log('=====');
console.log(this);
@nanha
nanha / gist:5073919
Last active December 14, 2015 10:39
https://github.com/felixge/node-formidable#example
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
@nanha
nanha / gist:5073900
Created March 3, 2013 00:25
passport qa
안녕하세요.
네 req.isAuthenticated() 함수를 사용하시면 됩니다.
위와 같이 작업하셔도 되고요. middleware 개념으로 작업을 하자면 아래와 같이도 구현할 수 있습니다.
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
@nanha
nanha / index.html
Last active December 14, 2015 01:59
KTH 자율출퇴근제 Comix
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cmx.io/v/0.1/cmx.css">
<script src="http://cmx.io/v/0.1/cmx.js" charset="utf-8"></script>
<body>
<div>
<scene id="scene1">
<label t="translate(0,346)">
<tspan x="0" y="0em">KTH 자율출퇴근제</tspan>
</label>
@nanha
nanha / gist:4760935
Created February 12, 2013 08:19
Da Young Shin 님에게.. 현재 님 소스에는 에러가 발생할 시에, 이를 화면에 표시해주는 if condition 처리가 되어 있지 않습니다. 1개의 예를 들어드릴께요.
/*
* GET home page.
*/
var mysql = require('mysql');
exports.index = function(req, res){
var client = mysql.createConnection({
host : '127.0.0.1',
user : '.....',
password : '.....',
database : 'Playground'
@nanha
nanha / gist:4754182
Created February 11, 2013 12:32
aop.js
var AOP = {
addBefore : function(obj, fname, before) {
var oldFunc = obj[fname];
obj[fname] = function() {
before(true, arguments, obj);
return oldFunc.apply(this,arguments);
};
},
addAfter : function(obj, fname, after) {
var oldFunc = obj[fname];
@nanha
nanha / gist:4727678
Created February 7, 2013 01:43
Twitter Archive 페이지에서 핵심인 application.min.js
var Grailbird = function (b, a, c) {
Grailbird.data = Grailbird.data || {};
Grailbird.data[b + "_" + a] = c
};
(function (b) {
var f = {}, e = {}, a = {};
var c = {
empty_month: Hogan.compile('<li class="without-tweets" title="" rel="tooltip" data-placement="bottom" data-date="" data-count="0"><span class="value">{{this_month}}</span></li>'),
month_bar: Hogan.compile('<li><a href="#" class="with-tweets" title="{{str_title}}: {{str_count}}" rel="tooltip" data-placement="bottom" data-idx="{{data_idx}}" data-date="{{str_title}}" data-count="{{this_count}}"><span class="bar" style="height: {{this_height}}%;"></span><span class="value">{{this_month}}</span></a></li>'),
header_str: Hogan.compile('{{title_str}} <span class="count">{{count}} {{content_type}}</span>'),