Node入门的一本电子书

Node入门

本书最后实现了一个简易Node应用,对于入门Node的同学,理解模块、阻塞、事件驱动等概念有很大帮助。

server.js

var formidable = require("formidable");
var http = require("http");
var url = require("url");

function start(route,handler) {
    function onRequest(req,res) {
        var postData = "";

        var pathname = url.parse(req.url).pathname;
        console.log("Request for " + pathname + " received.");
        
        if (pathname === "/favicon.ico") return;    

        route(handler,pathname,res,req);
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started for : http://localhost:8888");
}

exports.start = start;

router.js

function route(handle,pathname,res,req) {
    console.log("About to route a request for " + pathname);

    if (typeof handle[pathname] === "function") {
        return handle[pathname](res,req);
    } else {
        console.log("Error! No request handler found for " + pathname);
    }

}

exports.route = route;

reqHandlers.js

var querystring = require("querystring"),
    fs = require("fs"),
    formidable = require("formidable");

function start(res) {
    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">' +
        '<input type="submit" value="Upload file" />' +
        '</form>' +
        '</body>' +
        '</html>';
    
    res.writeHead(200,{"Content-Type":"text/html"});
    res.write(body);
    res.end();
}
function upload(res,req) {
    console.log("Request handler 'upload' was called.");
    
    // 可以试着打印req对象,查看如何解析的

    var form = new formidable.IncomingForm();
    console.log("form 正在解析 post");

    form.parse(req,function(error,fields,files) {
        console.log("解析完成");
        // 重命名图片,确保show正确
        console.log(files.upload.path);

        fs.renameSync(files.upload.path, "./tmp/test.png"); 

        res.writeHead(200, {"Content-Type": "text/html"});
        res.write("received image:<br/>");
        res.write("<img src='/show' />");
        res.end();
    })
}

function show(res) {
    console.log("Request handler 'show' was called");
    fs.readFile("./tmp/test.png","binary",function(error,file) {
        if (error) {
            res.writeHead(500, {"Content-Type": "text/plain"});
            res.write(error + "\n");
            res.end();
        } else {
            res.writeHead(200, {"Content-Type": "image/png"});
            res.write(file, "binary");
            res.end();
        }
    })
}

exports.start = start;
exports.upload = upload;
exports.show = show;

main.js

var server = require("./server");
var router = require("./router");
var reqhandlers = require("./reqHandlers");

var handler = {};
handler["/"] = reqhandlers.start;
handler["/start"] = reqhandlers.start;
handler["/upload"] = reqhandlers.upload;
handler["/show"] = reqhandlers.show;

server.start(router.route,handler);

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我等你,半年为期。逾期就狠狠把你忘记。我想明白了,人都有自己的命运,都有自己的缘分,我就安安静静的等着你,未来的日...
    七月阁阅读 3,247评论 4 0
  • 你问我,单身了多少年? 我说,好久! 其实,单身很多年,已经习惯了独来独往,一个人吃饭,一个人逛街,一个人看书,一...
    luocc阅读 1,410评论 0 0
  • 建立在电影与绘画造型基础上的动画艺术,是人类在镜头与银幕的世界里用绘画造型手段对客观世界进行记载、提炼与表达的审美...
    若多小课堂阅读 4,204评论 0 3
  • 记得当时年纪小,我爱谈天你爱笑。——卢前《本事》*岁月平淡如斯无聊时候居多似古旧的帛画是冰结的衰草总以一种闲看纷纷...
    陈余荛阅读 3,427评论 0 1