//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. uva 10129 play on words——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNUAAANeCAYAAAA1BjiHAAAgAElEQVR4nOydabWsuhaFywIasIAHJK

  2. 转载:HttpClient使用详解

    原文地址:http://blog.csdn.net/wangpeng047/article/details/19624529 Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自 ...

  3. 如何利用ZBrush中的DynaMesh创建身体(二)

    之前的ZBrush教程我们在了解了人体比例和结构的前提下,使用ZBrush®软件中的Append功能和InsertSphere笔刷添加躯干.本讲将参照图片继续对“亡灵僵尸”的形体结构进行细致刻画和使用 ...

  4. linux之间进程通信

    进程间通信方式:                    同主机进程间数据交换机制: pipe(无名管道) / fifo(有名管道)/ message queue(消息队列)和共享内存. 必备基础: f ...

  5. Eclipse中的快捷键总结

    Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.     ...

  6. Android学习----自适应国际化语言

    [前言] 自适应的知识与编程无关,关键在于配置文件的修改.自适应的内容包括:语言.屏幕.平台.今天就来说一下如何自适应国际化言. internationalization (国际化)简称:i18n,因 ...

  7. cotangent Laplacian

    几何网格处理经常用到 cotangent laplacian矩阵.前几天把这个功能整合到我的Maya 转 Matlab插件了. 这里发一个利用cotangent laplacian计算特征向量并显示的 ...

  8. ng-bind的使用

    由于JS是单线程的,当HTML页面执行alert的时候,会中断下面代码的运行,所以为了良好的用户体验,当需要在页面使用{{name}}的时候,通常不这样直接输出,而是用ng-bind绑定model数据 ...

  9. user database的initial size和dbcc shrinkfile

    之前我们讨论了dbcc shrinkfile改变tempdb initial size的情况.而用DBCC Shrinkfile去收缩一个user database,情况就比较简单了.让我们通过一些测 ...

  10. CUDA安装及配置:Windows 7 64位环境

    最近又有新的项目要做了,这次是关于CUDA---多核高性能计算的问题,所以最近一直在学习CUDA的编程问题,昨天安装软件完毕,运行第一个程序的时候还是遇到很多问题.所以这里给大家一起分享一下, 有和我 ...