Skip to content

Instantly share code, notes, and snippets.

@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 / node_express_middleware_simple.js
Created February 27, 2012 17:16
Express middleware simple example for node.js
/**
* @overview
*
* @author Caesar Chi
* @blog clonn.blogspot.com
* @version 2012/02/27
@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 / 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);
@clonn
clonn / html-bootsrap-start#.dash
Created October 29, 2012 08:08
html-bootsrap-start#
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>Bootstrap, from Twitter</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->