var http = require(‘http‘); http.createServer(function(req, res){ res.writeHeader(200, {Content-Type : ‘text/plain‘}); res.end(‘hello world!‘); }).listen(80);
function(req, res){ var method = req.method; if(method === ‘POST‘){ }else if(method === ‘GET‘){ }else if(method === ‘DELETE‘){ }else if(method === ‘PUT‘){ }else{ } }
var url = require(‘url‘); var fs = require(‘fs‘); function(req, res){ var pathname = url.parse(req.url).pathname; fs.readFile(path.join(ROOT, pathname), function(err, file){ if(err){ res.writeHeader(404); res.end(‘not Found File‘); return; } res.writeHeader(200); res.end(file); }); }
function(req, res){ var pathname = url.parse(req.url).pathname; var paths = pathname.split(‘/‘); var contronller = paths[1] || ‘index‘; var action = paths[2] || ‘index‘; var args = paths.slice(3); if(handler[contronller] && handler[contronller][action]){ handler[contronller][action].apply(null, [req, res].concat(args)); }else{ res.writeHeader(500); res.end(‘找不到响应控制器‘); } }
var url = require(‘url‘); var query = url.parse(req.url, true).query;//{foo : ‘aaa‘, baz : ‘bbb‘} req.query = query; handle(req, res);
function parseCookie(cookie){ var cookies = {}; if(!cookie){ return cookies; } var list = cookie.split(‘;‘); for(var i = 0,len = list.length;i < len;i++){ var pair = list[i].split(‘=‘); cookies[pair[0].trim()] = pair[1]; } return cookies; }; req.cookies = parseCookie(req.headers.cookie); handle(req, res); function serialize(name, value, options){ var pairs = [name+‘=‘+value]; if(options.expires){ pairs.push(‘Expires=‘+options.expires.toUTCString()); } if(options.maxAge){ pairs.push(‘Max-Age=‘+options.maxAge); } if(options.path){ pairs.push(‘Path=‘+options.path); } if(options.domain){ pairs.push(‘Domain=‘+options.domain); } if(options.httpOnly){ pairs.push(‘HTTPOnly‘); } if(options.secure){ pairs.push(‘Secure‘); } return pares.join(‘;‘); } res.writeHeader(‘Set-Cookie‘, serialize({isVisit : 1}));
var hasBody = function(req){ return ‘transfer-encoding‘ in req.Headers || ‘content-length‘ in req.Headers; } function(req, res){ if(hasBody(req)){ var buffers = []; req.on(‘data‘, function(chunk){ buffers.push(chunk); }); req.on(‘end‘, function(){ req.rawBody = Buffer.concat(buffers).toString(); handle(req, res); }); }else{ handle(req, res); } };
req.body = querystring.parse(req.rawBody); var mime = function(req){ return req.headers[‘content-type‘].split(‘;‘)[0] || ‘‘; } var xml2js = require(‘xml2js‘); var handle = function(req, res){ if(mime(req) === ‘application/json‘){ try{ req.body = JSON.parse(req.rowBody); }catch(){ res.writeHeader(‘400‘); res.end(‘invalid json‘); return; } } if(mime(req) === ‘application/xml‘){ xml2js.parseString(req,rawBody, function(err, xml){ if(err){ }else{ req.body = xml; } }); } };
var formidable = require(‘formidable‘); function(req, res){ if(hasBody(req)){ if(mime(req) === ‘multipart/form-data‘){ var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files){ req.body = fields; req.files = files; handle(req, res); }); } } }
var bytes = 1024; function(req, res){ var received = 0; var len = req.headers[‘content-length‘]?parseInt(req.headers[‘content-length‘], 10):null; if(len > bytes){ res.writeHeader(413); res.end; return; } res.on(‘data‘, function(chunk){ received += chunk.length; if(received > bytes){ req.destroy(); } }); handle(req, res); };
原文:http://www.cnblogs.com/hellohuman/p/3891038.html