Skip to content

Instantly share code, notes, and snippets.

@clonn
clonn / buffer2Stream.js
Created June 27, 2015 14:39
buffer2Stream
var stream = require('stream');
// Initiate the source
var bufferStream = new stream.PassThrough();
// Write your buffer
bufferStream.end(new Buffer('Test data.'));
// Pipe it to something else (i.e. stdout)
bufferStream.pipe(process.stdout);
@clonn
clonn / node_basic_http_rout.js
Created February 2, 2012 18:12
node.js, simple routing process
var server, ip = "127.0.0.1",
port = 1337,
http = require('http'),
url = require('url');
server = http.createServer(function (req, res) {
console.log(req.url);
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('hello world\n');
@clonn
clonn / index.html
Created February 7, 2012 15:49
node.js file read sample
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-TW" lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>node.js index html file</title>
</head>
<body>
<h1>node.js index html file</h1>
</body>
</html>
@clonn
clonn / form-serialize
Created February 22, 2012 18:51
form serialize for jQuery and YUI example.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="">
<meta name="created" content="2012-02-23">
<meta name="modified" content="Thu Feb 23 02:44:31 2012">
<title> Form serialize - Prototype by Caesar Chi</title>
<script type="text/javascript" src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
@clonn
clonn / node_express_basic.js
Created February 26, 2012 17:11
Node.js express simple route http server
/**
* @overview
*
* @author Caesar Chi
* @blog clonn.blogspot.com
* @version 2012/02/26
*/
// create server.
var app = require('express').createServer(),
@clonn
clonn / replaceMethod.js
Created May 2, 2012 11:17
JavaScript string template replace function
function format(format) {
if (!FB.String.format._formatRE) {
FB.String.format._formatRE = /(\{[^\}^\{]+\})/g;
}
var values = arguments;
return format.replace(
FB.String.format._formatRE,
function(str, m) {
@clonn
clonn / steps-1.md
Created October 6, 2015 09:00
create koding server

steps

term

cd Web/
touch server.js

server.js

@clonn
clonn / checked_file.js
Created July 3, 2012 04:07
Simple read file system on Node.js, let you know how to prevent checked file tiwce by curr time, prev time. Another is making you know how to get listener from watchFile API
var fs = require('fs');
var fullpath = '/home/clonn/test.html';
fs.watchFile(fullpath, function (curr, prev) {
if (curr.mtime.getTime() !== prev.mtime.getTime())
console.log('file update');
});
@clonn
clonn / connect-mongodb.js
Created October 23, 2012 08:22
demo how to connect mongoDB with URI on native mongo module
var mongo = require('mongodb');
var mongostr = 'mongodb://[user]:[pwd]@alex.mongohq.com:10046/[dbname]';
mongo.connect(mongostr, {}, function(error, db) {
if ( ! error)
console.log("connected, db: " + db);
});
@clonn
clonn / encrypt.js
Created October 24, 2012 03:27
encrypt md5/ sha1 by node.js
var connect = require('connect');
var uid = connect.utils.uid(20);
console.log('origin uid: ' + uid);
var crypto = require('crypto');
var sha1 = crypto.createHash('sha1');
var d = sha1.update(uid).digest('hex');
console.log('encrypt uid(sha1): ' + d);