今天花了多少时间在编程的学习上: 5
今天完成的事情:
- 参考node.js的入门的文章, 搭建起了一个上传图片并把图片展示到网站上面的应用。
- 整理了自己的node.js的文章在马克飞象
明天计划的事情:
- node.js其他的应用, 去买本书回来看一下
遇到的问题:
- querystring库的作用究竟是什么
- formidable库的作用究竟是什么
收获:
整理出来了一些在创建一个服务器的过程中可能会用到的代码
写入内容
console.log("Request handler 'start' was called.");
var body ='<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200,{"Content-type":"text/html"});
response.write(body);
response.end();
写入错误
response.writeHead(500,{"Content-Type":"text/plain"});
response.write(error +"\n");
response.end();
找不到
console.log("No request handler found for "+ pathname);
response.writeHead(404,{"Content-Type":"text/html"});
response.write("404 Not found");
response.end();
路由器
function route(handle, pathname, response, request){
console.log("About to route a request for "+ pathname);
if(typeof handle[pathname]==='function'){
handle[pathname](response, request);
}else{
console.log("No request handler found for "+ pathname);
response.writeHead(404,{"Content-Type":"text/html"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
index的代码
var server = require("./server");
var requestHandlers = require("./requestHandlers");
var router = require("./route");
var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/show"] = requestHandlers.show;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);
服务器
var http = require("http");
var url = require("url");
function start(route,handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for "+ pathname +" received.");
route(handle, pathname, response, request);
//解析网站传入的指令
//在控制台告诉我们接收到了什么样的指令
//将
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
解决解决了queryString库是做什么的
从名字就可以看出是一个和参数相关的帮助类,完成参数字符串和字符串或者是对象之间的转换。
网友评论