//http://www.nodebeginner.org/index-zh-cn.html#how-our-server-handles-requests 按照这个页面的例子写的,留作笔记
//index.js
var server = require('./server');
var route = require('./routes');
var requestHandlers = require("./requestHanlders"); var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show; server.start( route.route ,handle)

//server.js 基本的逻辑都在这边

var http = require('http');
var url = require('url'); function start( route, handle ){
http.createServer( function(request, response){
var pathname = url.parse( request.url).pathname;
console.log( pathname ); var postData = "";
/*
request.setEncoding("utf8");
request.addEventListener('data',function(chunk){
postData += chunk;
});
request.addEventListener('end',function(){
route(handle, pathname, response ,postData)
});
*/
request.setEncoding("utf8"); //addEventListener ? addListener
request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '"+
postDataChunk + "'.");
}); request.addListener("end", function() {
route(handle, pathname, response, postData);
}); //route( handle ,pathname ,response);
/*
response.writeHead(
200,
{
"Content-Type" : "text/plain"
}
);
response.write(data);
response.end();
console.log(' server is open');
*/
} ).listen(8000);
}; exports.start = start

//传数据到route文件

function route( handle, pathname ,response, postData){
console.log( "route a " + pathname );
if( typeof handle[pathname] === 'function'){
return handle[pathname]( response ,postData );
}else{
console.log('no Request')
return "404 NOT FOUND"
}
}; exports.route = route;

//展示结果页面的主要业务神马的

var querystring = require("querystring");
var fs = require("fs"); function start( response ,postData){
console.log("request handler start")
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>'; response.writeHead(200, {"Content-Type": "text/html"});
//response.writeHead(200, {"Content-Type": "text/plain"});
response.write(body);
response.end();
};
function upload( response ,postData){
console.log("request handler upload");
console.log( postData )
response.writeHead("200",{
"Content-Type" : "text/html"
});
response.write( querystring.parse(postData).text );
response.end()
};
function show(response, postData){
console.log('request img');
fs.readFile('./1.png','binary',function(err, data){
if(err){
response.writeHead(200,{
"Content-Type" : "text/html"
});
response.write('err');
response.end();
}else{
response.writeHead(200,{
"Content-Type" : "image/png"
//"Content-Type" : "image/png"
});
response.write(data,'binary');
response.end();
}
})
} exports.start = start;
exports.upload = upload;
exports.show = show;

Node_JS的更多相关文章

  1. 关于node_js的比较

    node_js的比较是我自己初学遇到的第一个绕脑的事情. 在比较的函数多了之后,一些函数的调用和变量提升, 搞得自己头晕,有时候函数是没有返回值的,自己还在 用变量值去比较,实际上却是undefine ...

  2. Travis CI用来持续集成你的项目

    这里持续集成基于GitHub搭建的博客为项目 工具: zqz@ubuntu:~$ node --version v4.2.6 zqz@ubuntu:~$ git --version git versi ...

  3. 手把手教从零开始在GitHub上使用Hexo搭建博客教程(四)-使用Travis自动部署Hexo(2)

    前言 前面一篇文章介绍了Travis自动部署Hexo的常规使用教程,也是个人比较推荐的方法. 前文最后也提到了在Windows系统中可能会有一些小问题,为了在Windows系统中也可以实现使用Trav ...

  4. 手把手教从零开始在GitHub上使用Hexo搭建博客教程(三)-使用Travis自动部署Hexo(1)

    前言 前面两篇文章介绍了在github上使用hexo搭建博客的基本环境和hexo相关参数设置等. 基于目前,博客基本上是可以完美运行了. 但是,有一点是不太好,就是源码同步问题,如果在不同的电脑上写文 ...

  5. travis CI

    travis可对多语言持续继承,本文以nodejs 为例. 首先添加文件.travis.yml 中language: node_jsnode_js:  - "6"  - " ...

  6. react-native start 运行流程

    在CMD下键入 C:\Node_JS\MyAwesomeProject>react-native start 运行流程: C:\Users\Grart\AppData\Roaming\npm\r ...

  7. 利用Travis CI 让你的github项目持续构建

    Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜.目前大多数的github项目都已经移入到Travis CI的构建队列 ...

  8. ubuntu下安装Node.js(源码安装)

    最近使用hexo的过程中出现了问题,中间载nodejs安装的时候也耽误了些许时间,所以在此记录一下安装的过程. 环境:ubuntu14.0.4LTS,安装nodejs版本node-v0.10.36.t ...

  9. [Javascript] Limit Built Branches on Travis

    By default, Travis will build all branches, tags, and Pull Requests. Because we're building our mast ...

随机推荐

  1. 边工作边刷题:70天一遍leetcode: day 85-1

    Inorder Successor in BST 要点:这题要注意的是如果不是BST,没法从树结构上从root向那边找p,只能遍历.而根据BST,可以只走正确方向 如果不检查right子树,可以从ro ...

  2. UESTC 878 温泉旅馆 --性质+枚举

    设FA为A的牌中数字异或和,FB为B的. 则有性质: ans = (所有的(A&B=0)个数 + (FA=FB且A&B=0)的个数)/2.即所有的FA>FB的个数(除2是因为这里 ...

  3. Finger Gestures 3.1

    3.x自定义手势 Finger Gestures用起来非常爽,除了有常用的手势之外,3.x的版本还增加了自定义手势! 官方Document:http://fingergestures.fatalfro ...

  4. 使用Unity开发Android的几种调试方法

    前言 本文举例几种Android 调试的方法(PS:我是通过unity引擎来开发安卓游戏) Eclipse + adt 查看LOG 1.为Eclipse 装上adt 插件 2.打开Eclipse 的L ...

  5. Unity3D面试题汇总

    1.请描述游戏动画有哪几种,以及其原理. 2.alpha blend 工作原理 3.写光照计算中的diffuse的计算公式 4.lod是什么,优缺点是什么 5.两种阴影判断的方法工作原理 6.MipM ...

  6. java 20 -1 递归的概述和案例

    /* * 递归:方法定义中调用方法本身的现象 * * 方法的嵌套调用,这不是递归. * Math.max(Math.max(a,b),c); * * public void show(int n) { ...

  7. css3爆炸效果更换图片轮播图

    思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...

  8. sublime配置全攻略

    大家好,今天给大家分享一款编辑器:sublime text2     我用过很多编辑器, EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.V ...

  9. js中接口的声明与实现

    实现接口,必须实现接口里的所有方法. function Interface(name,fns){//声明一个接口类            this.name = name;            th ...

  10. 应用python编写简单新浪微博应用(一)

    转载至:http://blog.sina.com.cn/s/blog_6c39196501016o7n.html 首先,你要有一个新浪微博账号. 申请页面:http://weibo.com 其次,你要 ...