博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node_JS
阅读量:5834 次
发布时间:2019-06-18

本文共 3346 字,大约阅读时间需要 11 分钟。

//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 = ''+        ''+        '
'+ ''+ ''+ '
'+ '
'+ '
'+ '
'+ ''+ ''; 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;

 

转载地址:http://uoucx.baihongyu.com/

你可能感兴趣的文章
LINQ To XML的一些方法
查看>>
C++成员初始化顺序
查看>>
[LeetCode] Copy List with Random Pointer
查看>>
openstack部署之nova
查看>>
QNX 线程 调度策略 优先级 时钟频率 同步
查看>>
day20-视图获取用户请求相关信息以及请求头
查看>>
JS组件系列——表格组件神器:bootstrap table
查看>>
存储过程Oracle(一)
查看>>
log4j日志归档
查看>>
Java笔记01——IO流
查看>>
mysql遇见error,1049
查看>>
uva 10034(最小生成树)
查看>>
Java随机生成不相等的号码
查看>>
NYOJ311 完全背包
查看>>
shp格式数据发布服务:postGIS + postgresql + geoserver
查看>>
101. Symmetric Tree - Easy
查看>>
codevs——2822 爱在心中
查看>>
标准C++中的string类的用法总结
查看>>
生成随机图片验证码
查看>>
Python基础班---第一部分(基础)---Python基础知识---认识Python
查看>>