当前位置: 代码迷 >> JavaScript >> NODEJS(二)File Upload Sample with routers and handlers
  详细解决方案

NODEJS(二)File Upload Sample with routers and handlers

热度:1068   发布时间:2012-10-26 10:30:58.0
NODEJS(2)File Upload Sample with routers and handlers
NODEJS(2)File Upload Sample with routers and handlers

What's needed to "route" requests?
We need to be able to feed the requested URL and possible additional GET and POST parameters into our router, and based on these the router then needs to be able to decide which code to execute(a collection of request handlers that do the actual work when a request is received).

We need other modules in Node.js namely url and querystring.

url module provide methods which extract the URL, and querystring module for query string.

We do some changes in server.js as follow, we can get the requestpath in our URL.
var url = require("url");
...snip...
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
We get these messages on console then:
Request for / received.
Request for /luohua received.
Request for /order.do received.

We create a router.js with these content:
function route(pathname){
            console.log("About to route a request for " + pathname);   
}

exports.route = route;

Pass the function method name to our route and start, refactor server.js as follow:
function start(routeMethod) {
...snip...
routeMethod(pathname);
...snip...

index.js will be changed as follow:
var server = require("./server");
var router = require("./router");

server.start(router.route);

Execution in the kingdom of verbs
Routing to real request handlers
Create a module requestHandlers.js as follow:
function start(){
            console.log("Request handler 'start' was called.");   
}

function upload(){
            console.log("Request handler 'upload' was called.");
}

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

In JavaScript, objects are just collections of name/value pairs, think of a JavaScript object as a dictionary with string keys.
We use a key/value pair, put the functions in value, and pass this map to our router.
index.js
var requestHandlers = require("./requestHandlers");

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

server.start(router.route, handle);

server.js
function start(routeMethod, handleMap) {
...snip...
routeMethod(handleMap,pathname);
...snip...

router.js
function route(handleMap,pathname){
            console.log("About to route a request for " + pathname);   
            if(typeof handleMap[pathname] === 'function'){
                        handleMap[pathname](); 
            } else {
                        console.log("No request handler found for " + pathname);
            }
}

Making the request handlers respond
How to not do it
We will write back the content in response
requestHandlers.js
function start(){
            console.log("Request handler 'start' was called.");   
            return "Hello Start";
}

function upload(){
            console.log("Request handler 'upload' was called.");
            return "Hello Upload";     
}

router.js
            if(typeof handleMap[pathname] === 'function'){
                        return handleMap[pathname]();   
            } else {
                        console.log("No request handler found for " + pathname);
                        return "404 Not found";
            }

server.js
response.writeHead(200, {"Content-Type": "text/plain"});
    var content = routeMethod(handleMap,pathname);
    response.write(content);
...snip..

Blocking and non-blocking
We will try to make blocking in request handler, make it wait 10 seconds before returning its "Hello Start" string.

I download the mac version here, Node was installed at /usr/local/bin/node, npm was installed at /usr/local/bin/npm.

And I change the requestHanders.js as follow:
function start(){
  …snip…
  function sleep(milliSeconds){
    var startTime = new Date().getTime();
    while (new Date().getTime() < startTime + milliSeconds);
  }
 
  sleep(10000);
  return "Hello Start";
}

From the code, we can understand that start will take 10 seconds to sleep. But upload will response immediately.
But we hit the URL http://localhost:8888/start first, and then in another tab, hit the URL http://localhost:8888/upload. But both of the URLs are waiting for 10 seconds.

In fact, Node.js is single-threaded. So we will try to do it no-blocking ways. This way we are saying " probablyExpensiveFunction(), please do your stuff, but the single Node.js thread will not wait right here until you are finished, I will continue to execute the lines of code below you. So would you please take this callbackFunction() here and call it when you are finished doing your expensive stuff.

non-blocking exec module
var exec = require("child_process").exec;

function start() {
  console.log("Request handler 'start' was called.");
  var content = "empty";

  exec("ls -lah", function(error, stdout, stderr) {
    content = stdout;
  });
  return content;
}

But this can only show the empty on the page. Because exec really is non-blocking.

Responding request handlers with non-blocking operations
pass the response to route handler in server.js
…snip…
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(handle, pathname, response);  
}
…snip...

router.js will handle the response and pass the response to requestHanders.
…snip…
function route(handle, pathname, response) {
  console.log("About to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
    handle[pathname](response);
  } else {
    console.log("No request handler found for " + pathname);
    response.writeHead(404, {"Content-Type": "text/plain"});
    response.write("404 Not Found");
    response.end();
  }
}
…snip…

Handle the response in requestHandlers.js

exec("ls -lah", function(error, stdout, stderr) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write(stdout);
    response.end();
});
…snip…
function upload(response) {
  console.log("Request handler 'upload' was called.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello Upload");
  response.end();
}

This time everything works fine and we can see the file list under that directory.

references:
http://www.nodebeginner.org/

  相关解决方案