《Node.js入门》总结

读完这本书之后,对作者相当的佩服,推荐作为Node.js的入门资料。还没看的同学可以<a href="http://www.nodebeginner.org/index-zh-cn.html">去看看</a>。
本书以服务器处理图片上传并展示的例子,从一开始就使用更好的封装去编写。从模块上来看,有一下四个文件:

index.js: node入口文件
server.js: 服务器模块,提供端口的监听,基本的设置等
router.js: 请求路由,分发请求
requestHandler,js: 处理具体请求的模块

Node.js通过调用require获取对应模块的方法,index.js文件中引用其他的三个文件:

var server = require('./nodeServer');
var router = require('./router');
var requestHandles = require('./requestHandles');

同时将请求接口的映射传入到server中,并且将路由转换的方法传入

var handle = {};
handle["/"]  = requestHandles.start;
handle["/start"] = requestHandles.start;
handle["/upload"] = requestHandles.upload;
handle["/show"] = requestHandles.show;

server.start(router.route, handle);

server.js

server.js只是负责把请求的函数发送给路由处理,让路由分配到具体的请求处理类去,这里可以使用http去解析请求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.");
}

requestHandler.js

利用response中的'writeHead', 'write', 'end'等方法返回给调用者数据:

response.writeHead(200, {"Content-Type": "image/jpg"});
response.write(file, "binary");
response.end();

文件上传使用的是formidable,使用fs用来上传和获取文件数据。文章在start方法中有个提交表单的html:

function start(response, postData) {
    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>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

上传文件并将文件名改成自己定义的名称:

fs.renameSync(files.upload.path, "your'file name");

当我们将文件上传成功之后会有反馈

received upload:

{ fields: { title: 'Hello World' },
  files:
   { upload:
      { size: 1558,
        path: '/tmp/1c747974a27a6292743669e91f29350b',
        name: 'us-flag.png',
        type: 'image/png',
        lastModifiedDate: Tue, 21 Jun 2011 07:02:41 GMT,
        _writeStream: [Object],
        length: [Getter],
        filename: [Getter],
        mime: [Getter] } } }

文件读取:

fs.readFile("your's file name", functon({ function body }));

本文只是作为一个笔记,如果想了解详情请移步<a href="http://www.nodebeginner.org/index-zh-cn.html">这里</a>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,853评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,930评论 25 708
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,491评论 1 37
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    Myselfyan阅读 4,088评论 2 58
  • github地址,欢迎大家提交更新。 express() express()用来创建一个Express的程序。ex...
    Programmer客栈阅读 2,588评论 0 1