Node_JS
//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的更多相关文章
- 关于node_js的比较
node_js的比较是我自己初学遇到的第一个绕脑的事情. 在比较的函数多了之后,一些函数的调用和变量提升, 搞得自己头晕,有时候函数是没有返回值的,自己还在 用变量值去比较,实际上却是undefine ...
- Travis CI用来持续集成你的项目
这里持续集成基于GitHub搭建的博客为项目 工具: zqz@ubuntu:~$ node --version v4.2.6 zqz@ubuntu:~$ git --version git versi ...
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(四)-使用Travis自动部署Hexo(2)
前言 前面一篇文章介绍了Travis自动部署Hexo的常规使用教程,也是个人比较推荐的方法. 前文最后也提到了在Windows系统中可能会有一些小问题,为了在Windows系统中也可以实现使用Trav ...
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(三)-使用Travis自动部署Hexo(1)
前言 前面两篇文章介绍了在github上使用hexo搭建博客的基本环境和hexo相关参数设置等. 基于目前,博客基本上是可以完美运行了. 但是,有一点是不太好,就是源码同步问题,如果在不同的电脑上写文 ...
- travis CI
travis可对多语言持续继承,本文以nodejs 为例. 首先添加文件.travis.yml 中language: node_jsnode_js: - "6" - " ...
- react-native start 运行流程
在CMD下键入 C:\Node_JS\MyAwesomeProject>react-native start 运行流程: C:\Users\Grart\AppData\Roaming\npm\r ...
- 利用Travis CI 让你的github项目持续构建
Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜.目前大多数的github项目都已经移入到Travis CI的构建队列 ...
- ubuntu下安装Node.js(源码安装)
最近使用hexo的过程中出现了问题,中间载nodejs安装的时候也耽误了些许时间,所以在此记录一下安装的过程. 环境:ubuntu14.0.4LTS,安装nodejs版本node-v0.10.36.t ...
- [Javascript] Limit Built Branches on Travis
By default, Travis will build all branches, tags, and Pull Requests. Because we're building our mast ...
随机推荐
- 【C#】2.算法温故而知新 - 冒泡排序
冒泡排序可以很好的解决前面提到的简单桶排序的2个问题,冒泡排序的基本思想是:每次比较两个相邻的元素,如果它们的顺序错误就把它们交换过来. 该算法的核心部分是双重嵌套循环,其时间复杂度是O(N²). 缺 ...
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
题意:给n个数字,每次两种操作: 1.修改第x个数字为y. 2.查询[L,R]区间内第D位为P的数有多少个. 解法:这题当时被卡内存了,后来看了下别人代码发现可以用unsigned short神奇卡过 ...
- [转] Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
from: http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得 ...
- VC2010中去掉红绝下划线
1. 在VC2010 的tools/中找到editior. 已经找到c/c++->advanced->IntelliSense->Disable IntelliSence->T ...
- JavaScript基于时间的动画算法
转自:https://segmentfault.com/a/1190000002416071 前言 前段时间无聊或有聊地做了几个移动端的HTML5游戏.放在不同的移动端平台上进行测试后有了诡异的发现, ...
- mobileTech
A useful tools or tips list for mobile web application developing 这个项目收集移动端开发所需要的一些资源与小技巧 工具类网站 HTML ...
- Hashtable 数据遍历的几种方式
Hashtable 在集合中称为键值对,它的每一个元素的类型是 DictionaryEntry,由于Hashtable对象的键和值都是Object类型,决定了它可以放任何类型的数据, 下面我就把Has ...
- Java 操作 Redis 高级
/Users/sherry/WorkPath/Git/Web/redisDemo/src/main/java/org/zln/utils/JedisUtils.java package org.zln ...
- .net程序集强命名(签名)
要想得到强签名的dll有两种情况: 1.给项目添加强命名 在你的项目右键->属性->签名,勾选“为程序集签名”,新建 或 浏览已经新建过的.pfx文件,然后重新build项目,生成的dll ...
- ORACLE SELECT INTO NO_DATA_FOUND问题
存储过程中使用了类似如下语句: SELECT col INTO v_col FROM t_table 当查询不到记录时,会出现“数据未发现”的异常 解决方法: (1)使用MAX函数 SELECT MA ...