Node.js入门 读完本书之后,你将完成一个完整的web应用,该应用允许用户浏览页面以及上传文件。
里面对一些知识的讲解,让你略窥Node.js的门径。最好一段代码一段代码的写下来,我的习惯是手里拿着笔,边写重点的词,边思索,再敲敲代码。一天到两天你能看完这本书,我看过一遍了,计划再寻摸一遍。有问题大家可以一起探讨。
四个文件在同一个目录下
启动
cmd窗口:
输入 node D:\workspace\nodejs\index.js
也可以定位到当前目录后:
D:\workspace\nodejs\ node index.js
打开
localhost:8080/start
说下,此书最后的例子完整代码中有点问题。你应该会出现 类似no such file or directory ,或者是此文件已经存在的错误提示。修改处已经做了标注:
具体文件:
index.js
var server=require("./server"); var router=require("./router"); var requestHandlers=require("./requestHandlers"); var handle={}; handle["/"]=requestHandlers.start; handle["/start"]=requestHandlers.start; handle["/upload"]=requestHandlers.upload; handle["/show"]=requestHandlers.show; server.start(router.route,handle);
server.js
var http = require("http"); var url=require("url"); function start(route,handle){ function onRequest(request,response){ var postData=""; var pathname=url.parse(request.url).pathname; console.log("Request for"+pathname+" received"); route(handle,pathname,response,request); } http.createServer(onRequest).listen(8080); console.log("Server has started"); } exports.start=start;
router.js
var http = require("http"); var url=require("url"); function start(route,handle){ function onRequest(request,response){ var postData=""; var pathname=url.parse(request.url).pathname; console.log("Request for"+pathname+" received"); route(handle,pathname,response,request); } http.createServer(onRequest).listen(8080); console.log("Server has started"); } exports.start=start;
requestHandler.js
//var exec=require("child_process").exec; var querystring=require("querystring"), fs=require("fs"), formidable=require("formidable"); function start(response){ console.log("start was called"); var body="<!DOCTYPE html>"+ '<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>'+ '这是个测试</title></head><body>'+ '<form action="/upload" enctype="multipart/form-data" method="post">'+ '<input type="file" name="upload" />'+ '<input type="submit" value="Upload file" />'+ '</form>'+'</body></html>'; response.writeHead(200,{"Content-Type":"text/html"}); response.write(body); response.end(); } function upload(response,request){ console.log("upload was called"); var form=new formidable.IncomingForm({uploadDir:"tmp"});//Modify console.log("about to parse"); form.parse(request,function(error,fields,files){ console.log("parsing done"); fs.renameSync(files.upload.path,"tmp/test.jpg");//The Dir,pay attention response.writeHead(200, {"Content-Type": "text/html"}); response.write("Received image:<br /><img src='/show' />"); response.end(); }); } function show(response){ console.log("Show was callde"); fs.readFile("tmp/test.jpg","binary",function(error,file){//The Dir,pay attention if(error){ response.writeHead(500,{"Content-Type":"text/plain"}) response.write(error + "\n"); response.end(); } else { response.writeHead(200, {"Content-Type": "image/jpeg"}); response.write(file, "binary"); response.end(); } }) } exports.start=start; exports.upload=upload; exports.show=show;
你可以下载 node.js入门正确示例