吉吉于

free

基于Node.js的上传模块

Node.js下实现哪怕很简单的东西都要自己写,不过实现起来不是很难,不像PHP那样把很多底层的东西封装好供你使用。

这道理也类似于C、C++和C#吧,大公司没人要C#,我考虑是不是应该开始复习C++了,废话不多说,这节聊Node.js。

关于Node.js,最好还是自己查一下,扫扫盲。

我是去年看到一个中山大学大三计算机系的孩子在玩这个东西,还有各种偶然碰到它的时候,那时候在玩THREE.JS,没时间看它,最近鼓捣了一下,很爽。

单线程,并发,事件驱动,无阻塞,并且构建在强大的V8引擎之上,还能构建一个comet系统,这都让人”兽血沸腾”。(哈哈)

有一句话来描述Node.js的并发模型:一切都是并发的,除了你的代码。

慢慢理会这句话吧,这里推荐给你一片文章:http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb

玩Node.js,需要安装相应环境,现在的孩子幸福啊,不用安装cygwin模拟linux环境了,Node已经发布支持Windows的版本了,直接安装就Ok。

完了还可以通过Web Matrix进行开发MVC模式的Node.js 程序。

前者就好比写Console程序,后者就是可视化性强了点,根据各自喜好配置咯。

OK,细节方面我就不说了,想学的娃娃们自己动手吧,过程最重要了是不。

分享一下资源:

node.js中文社区:  http://cnodejs.org/

Node.js入门教程(E文): http://nodebeginner.org/

中文用户手册:  http://cnodejs.org/cman/

node.js API文档:  http://cnodejs.org/api/

npm依赖管理工具(node 中的RubyGems):   http://npmjs.org/

Node Cloud(全面的node.js资料库): http://www.nodecloud.org/

下边贴个代码,不是我写的,我也在学习中。

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 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;

route.js

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;

requestHandler.js

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

function start(response) {
  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();
}

function upload(response, request) {
  console.log("Request handler 'upload' was called.");

  var form = new formidable.IncomingForm();
  console.log("about to parse");
  form.parse(request, function(error, fields, files) {
    console.log("parsing done");

    /* Possible error on Windows systems:
       tried to rename to an already existing file */
    fs.rename(files.upload.path, "/tmp/test.png", function(err) {
      if (err) {
        fs.unlink("/tmp/test.png");
        fs.rename(files.upload.path, "/tmp/test.png");
      }
    });
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("received image:<br/>");
    response.write("<img src='/show' />");
    response.end();
  });
}

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

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

 

 

 

 

转载请注明:于哲的博客 » 基于Node.js的上传模块