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 ...
随机推荐
- 边工作边刷题:70天一遍leetcode: day 79
3Sum Smaller 要点:类似的题还有lintcode的triangle count:https://github.com/delbao/onlineJudge/blob/master/lint ...
- hdu-5495 LCS(置换)
题目链接: LCS Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- UVA 12532 Interval Product
线段树水题,忽略每个数的大小,只记住他们的正负即可,规规矩矩的代码.不过这是我第一次完全自己写的一次A的代码了.纪念一下... #include <iostream> #include & ...
- C++学习之开发环境搭建篇(一)
由于C++是一门非跨平台语言,其开发的程序编译生成的可执行文件,只能在相应的操作系统中被执行,离开此系统环境将无法执行. 主要原因是不同的操作系统,可执行文件的结构不同,最为常见的操作系统是有:MAC ...
- SpringMVC常用接收Json的两种方法
@RequestBody JSONObject requestJson @RequestBody User user 一种是自定义加注解,由Spring负责绑定,一种是使用通用的JSONObject
- Mybaits学习总结2
http://www.cnblogs.com/xdp-gacl/p/4262895.html 继续参考这篇文章写Mybaits学习总结 上一章,我修改了编码,统一为UTF8之后,便没有编码错误 < ...
- 12个JavaScript技巧
转自:http://web.jobbole.com/86146/ 在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操 ...
- 学习node.js 第4篇 建立一个最小的web聊天系统
我们生活在一个实时的世界里,有什么比聊天更加实时吗?那就让我们先写一个基于TCP 的聊天服务器吧,并且支持Telnet 连接.这很容易,而且能够完全用Node来编写.首先,我们需要在Node 中包含T ...
- 一些正则验证-JS
Validation = { textCount: function(field, counter, maxLimit) { var message = $(field).val(); if ($(f ...
- Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...