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 ...
随机推荐
- 1.素数判定(如何输出\n,\t,不用关键字冲突)
题目描述 Description 质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数. 素数在数论中有着很重要的地位.比1大但不是素数的数称为合数.1和0既非素数也 ...
- DOTween文档
前言 DOTween现在还处于 alpha,所以还有一些缺失的功能(如路径插件,附加回调和其它的tween选项),这个文档在不久的将来可能会更新. DoTween:0.8.2.00 官方文档:http ...
- IE插件收集
IEWatch IEWatch是一个微软IE的内置插件,可以让你看到和分析HTTP/HTTPS头信息,Cookies以及通过GET和POST提交的数据.我是经常用来看页面加载时间 下载最新版本请访问: ...
- 只有 DBA 才能导入由其他 DBA 导出的文件
两句话搞定问题: grant dba to testuser ; 如果还不行,再执行: alter user testuser default role DBA:
- 转: Github访问慢解决办法
from: https://yq.aliyun.com/articles/36744 Github访问慢解决办法 zxiaofan 2016-04-20 17:25:00 浏览2156 评论0 摘 ...
- finder怎么才能找到library
右键Finder——前往目录 输入~/Library
- HTML5-WebSocket技术学习(1)
WebSocket是为解决客户端与服务端实时通信而产生的技术. 介绍它是什么的废话不多说了,直接说怎么用: 客户端: 1.创建一个 EventSource 对象 var es = new EventS ...
- Saltstack-初体验
安装 rpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm yum install s ...
- html2canvas手机端模糊问题
待解决.测试对于图片之类的没有影响.但是文字在手机上看起来比较模糊.
- 13Mybatis_SqlMapConfig.xml专题讲解
Mybatis的SqlMapConfig.xml中以下的标签: properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandlers(类型处理器 ...