Status of this book
What you are reading is already the final version of the book. Therefore, updates will only be made when error corrections and corresponding corrections are made to the new version of Node.js.
The code cases in this book have been tested in Node.js version 0.6.11 and can work correctly.
Reader object
This book is best for readers with a technical background similar to me: at least have some experience in an object-oriented language such as Ruby, Python, PHP, or Java; is in the beginning of JavaScript and is completely a newbie in Node.js .
This refers to developers who have some experience in other programming languages. It means that this book will not introduce very basic concepts such as data types, variables, control structures, etc. To understand this book, I assume that you already know these basic concepts.
However, this book will introduce functions and objects in JavaScript in detail, because they are very different from functions and objects in other similar programming languages.
Structure of this book
After reading this book, you will complete a complete web application that allows users to browse pages and upload files.
Of course, the application itself is nothing great. Compared to the code itself written to implement this function, we are more concerned with how to create a framework to cleanly strip the different modules of our application. Isn't it very mysterious? You'll understand later.
This book starts with introducing the differences between JavaScript development in the Node.js environment and JavaScript development in the browser environment.
Immediately afterwards, we will lead everyone to complete the most traditional "Hello World" application, which is also the most basic Node.js application.
Finally, I will discuss with you how to design a "really" complete application, analyze the different modules that need to be implemented to complete the application, and introduce how to implement these modules step by step.
What is guaranteed is that in this process, you will learn some advanced concepts in JavaScript, how to use them, and why these concepts can be implemented while similar concepts in other programming languages cannot be implemented.
All source codes of this application can be accessed through the Github code repository in this book: https://github.com/ManuelKiessling/NodeBeginnerBook/tree/master/code/application.
JavaScript and Node.js
JavaScript and you
Putting technology aside, let’s talk about you and your relationship with JavaScript first. The main purpose of this chapter is to let you see if it is necessary for you to continue reading the contents of subsequent chapters.
If you are like me, then you started to "develop" with HTML very early. Because of this, you came across this interesting thing called JavaScript, and for JavaScript, you only know the basic operations - add interactions to web pages .
What you really want is "dry stuff", you want to know how to build complex web sites - so you learn a programming language such as PHP, Ruby, Java, and start writing "back-end" code.
At the same time, you are always paying attention to JavaScript. With some introductions to technologies such as jQuery and Prototype, you have gradually learned a lot of advanced skills in JavaScript, and at the same time you feel that JavaScript is not just window.open () That's so simple. .
However, these are all front-end technologies after all. Although using jQuery always makes you feel good when you want to enhance your page, in the end, you are at most a JavaScript user, not a JavaScript developer.
Then, Node.js, JavaScript on the server, how cool is this?
So, you feel it's time to pick up both familiar and unfamiliar JavaScript again. But don't worry, writing Node.js applications is a thing; understanding why they need to be written in the way they write means that you need to understand JavaScript. This time I played it real.
Here comes the problem: Since JavaScript really exists in two, or even three forms (from small toys that enhance DHTML in the 1990s, to front-end technology in the strict sense like jQuery, and until now ) Therefore, it is difficult to find a "right" way to learn JavaScript, so that you can feel like you are really developing it rather than just using it when writing Node.js applications.
Because that's the key: You are already an experienced developer, and you don't want to learn new technologies by looking for solutions everywhere (and there may be incorrect ones), you have to make sure you are learning the right way This technology.
Of course, there are many excellent JavaScript-learning articles outside. However, sometimes it is far from enough to rely on those articles alone. What you need is guidance.
The goal of this book is to provide you with guidance.
Short statement
There are very good JavaScript programmers in the industry. And I am not one of them.
I am the one described in the previous section. I'm familiar with how to develop backend web applications, but I'm just a newbie with "real" JavaScript and Node.js. I have just recently learned some advanced JavaScript concepts and have no practical experience.
Therefore, this book is not a book that "from introductory to mastery", but more like a book that "from introductory to advanced".
If successful, then this book is the tutorial I most hoped to have when I started learning Node.js.
Server JavaScript
JavaScript was first run in a browser, however, the browser just provides a context that defines what can be done with JavaScript, but does not "say" much about what the JavaScript language itself can do. In fact, JavaScript is a "complete" language: it can be used in different contexts, with its capabilities even greater than other similar languages.
Node.js is actually another context, which allows JavaScript code to be run on the backend (out of the browser environment).
To implement JavaScript code running in the background, the code needs to be interpreted first and then executed correctly. This is exactly the principle of Node.js, which uses Google's V8 virtual machine (the JavaScript execution environment used by Google's Chrome browser) to interpret and execute JavaScript code.
In addition, there are many useful modules accompanying Node.js that can simplify a lot of repetitive work, such as outputting strings to the terminal.
Therefore, Node.js is actually both a runtime environment and a library.
To use Node.js, you need to install it first. I won't go into details about how to install Node.js here. You can directly refer to the official installation guide. After the installation is complete, continue to come back and read the content below this book.
“Hello World”
Okay, let’s not say much “nonsense”, let’s start our first Node.js application: “Hello World” immediately.
Open your favorite editor and create a helloworld.js file. We have to do it to output "Hello World" to STDOUT, as follows:
The code copy is as follows: console.log("Hello World");
Save the file and execute it through Node.js:
Copy the code as follows: node helloworld.js
If normal, Hello World will be output on the terminal.
OK, I admit that this application is a bit boring, so let’s take a little “dry”.
A complete web application based on Node.js
Use Cases
Let's set the goal simple, but it must be practical:
1. Users can use our app through their browser.
2. When the user requests http://domain/start, he can see a welcome page with a file upload form on the page.
3. The user can select an image and submit a form. The file will then be uploaded to http://domain/upload. After the page is uploaded, the image will be displayed on the page.
It's almost done, you can go to Google now and find something to mess around to complete the function. But we won't do this for now.
Going further, in the process of accomplishing this goal, we need more than just basic code regardless of whether the code is elegant or not. We also need to abstract this to find a way to build more complex Node.js applications.
Application of different modules
Let’s break down this application. In order to implement the above use case, what parts do we need to implement?
1. We need to provide a web page, so we need an HTTP server
2. For different requests, our server needs to give different responses according to the request URL, so we need a route to correspond to the request handler.
3. When the request is received by the server and passed through the route, it needs to be processed, so we need the final request handler.
4. The routing should also be able to process POST data and encapsulate the data into a more friendly format and pass it to the request processing into the program, so the request data processing function is required.
5. We not only need to process the request corresponding to the URL, but also display the content, which means we need some view logic for the request handler to send the content to the user's browser.
6. Finally, the user needs to upload the image, so we need to upload the processing function to handle the details of this aspect.
Let’s first think about how we will build this structure if we use PHP. Generally speaking, we will use an Apache HTTP server and match it with the mod_php5 module.
From this perspective, the entire requirement of "receiving HTTP requests and serving web pages" does not require PHP to handle at all.
However, for Node.js, the concept is completely different. When using Node.js, we are not only implementing one application, but also implementing the entire HTTP server. In fact, our web applications and corresponding web servers are basically the same.
It sounds like there is a lot of work to do, but then we will gradually realize that this is not a hassle for Node.js.
Now let’s start the implementation path, starting with the first part - the HTTP server.
Build the module for the application
A basic HTTP server
When I was about to start writing my first "real" Node.js application, I not only didn't know how to write Node.js code, but also how to organize it.
Should I put everything in one file? There are many tutorials online that will teach you to put all the logic into a basic HTTP server written in Node.js. But what if I want to add more content while also keeping the code readable?
In fact, it is quite simple to keep the code separated as long as you put the code of different functions into different modules.
This method allows you to have a clean main file that you can execute with Node.js; at the same time you can have clean modules that can be called by the main file and other modules.
So, now let's create a main file for starting our application and a module that holds our HTTP server code.
In my impression, calling the main file index.js is more or less a standard format. It is easy to understand to put the server module into a file called server.js.
Let's start with the server module. Create a file called server.js in the root directory of your project and write the following code:
The code copy is as follows:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
Get it done! You just finished a working HTTP server. To prove this, let's run and test this code. First, execute your script with Node.js:
node server.js
Next, open the browser and visit http://localhost:8888/, and you will see a web page with "Hello World" written on it.
This is interesting, isn't it? Let's talk about the issue of HTTP server first, put the matter of how to organize the project aside. What do you think? I promise we will solve that problem later.
Analyze HTTP servers
Then, let's analyze the composition of this HTTP server.
The first line requests (requires) the http module that comes with Node.js and assigns it to the http variable.
Next we call the function provided by the http module: createServer. This function will return an object, which has a method called listen. This method has a numerical parameter that specifies the port number that the HTTP server listens for.
Let's ignore the function definition in the parentheses of http.createServer for the time being.
We could have used code like this to start the server and listen for port 8888:
The code copy is as follows:
var http = require("http");
var server = http.createServer();
server.listen(8888);
This code will only start a server that listens to port 8888, it does nothing else and will not even answer the request.
The most interesting (and, if you were used to using a more conservative language like PHP, it's weird) is the first argument to createSever(), a function definition.
In fact, this function definition is the first and only parameter of createServer(). Because in JavaScript, functions can be passed like other variables.
Perform function pass
For example, you could do this:
The code copy is as follows:
function says(word) {
console.log(word);
}
function execute(someFunction, value) {
someFunction(value);
}
execute(say, "Hello");
Please read this code carefully! Here, we pass the say function as the first variable of the execute function. What is returned here is not the return value of say, but the saying itself!
In this way, say becomes the local variable someFunction in execute . execute can use the say function by calling someFunction() (in parentheses).
Of course, because say has a variable, execute can pass such a variable when calling someFunction.
We can, like we did just now, pass a function as a variable with its name. But we don't have to circle around this "define first, then pass" circle. We can directly define and pass this function in parentheses of another function:
The code copy is as follows:
function execute(someFunction, value) {
someFunction(value);
}
execute(function(word){ console.log(word) }, "Hello");
We directly define the function we are going to pass to execute where the first parameter is accepted by execute.
In this way, we don't even have to name this function, which is why it is called anonymous function.
This is our first close contact with what I think is "advanced" JavaScript, but we still have to go step by step. Now let's accept this first: In JavaScript, a function can receive a parameter as another function. We can first define a function and then pass it, or we can directly define the function where the parameters are passed.
How does function pass-through make HTTP server work
With this knowledge, let’s take a look at our simple but not simple HTTP server:
The code copy is as follows:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
Now it should look much clearer: we passed an anonymous function to the createServer function.
The same purpose can be achieved using such code:
The code copy is as follows:
var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
Maybe now we should ask this question: Why do we use this method?
Event-driven callbacks
This question is hard to answer (at least for me), but this is how Node.js works natively. It's event-driven, which is why it's so fast.
You might want to take some time to read Felix Geisendörfer's masterpiece Understanding node.js, which introduces some background knowledge.
It all comes down to the fact that "Node.js is event driven". Well, I don't really understand the meaning of this sentence. But I will try to explain why it makes sense for us to write web applications using Node.js.
When we use the http.createServer method, we certainly not just want a server that listens to a certain port, we also want it to do something when the server receives an HTTP request.
The problem is, this is asynchronous: the request can arrive at any time, but our server runs in a single process.
When writing PHP applications, we are not worried about it at all: any time a request enters, the web server (usually Apache) creates a new process for the request and starts executing the corresponding PHP script from beginning to end.
So in our Node.js program, when a new request reaches port 8888, how do we control the process?
Well, that's where Node.js/JavaScript's event-driven design can really help - although we still have to learn some new concepts to master it. Let's see how these concepts are applied to our server code.
We created the server and passed a function to the method that created it. Whenever our server receives a request, this function will be called.
We don't know when this will happen, but we now have a place to handle the request: it is the function we passed in the past. As for whether it is a predefined function or anonymous function, it doesn't matter.
This is the legendary callback. We pass a function to a method, which calls this function to call back when a corresponding event occurs.
At least for me, it takes some effort to understand it. If you are still not sure, read Felix's blog posts.
Let's think about this new concept again. How do we prove that after creating the server, our code will continue to be valid even if no HTTP request comes in and our callback function is not called? Let's try this:
The code copy is as follows:
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
Note: Where onRequest (our callback function) is triggered, I output a piece of text using console.log. After the HTTP server starts working, a piece of text is also output.
When we run it node server.js as usual, it immediately outputs "Server has started." on the command line. When we make a request to the server (visit http://localhost:8888/ in the browser), the message "Request received." will appear on the command line.
This is the event-driven asynchronous server-side JavaScript and its callbacks!
(Note that when we access a web page on the server, our server may output "Request received." twice. That's because most servers will try to read http when you visit http://localhost:8888/: //localhost:8888/favicon.ico )
How does the server handle requests
OK, let's briefly analyze the remaining part in our server code, that is, the main part of our callback function onRequest().
When the callback is started and our onRequest() function is triggered, two parameters are passed in: request and response.
They are objects, you can use their methods to handle the details of HTTP requests and respond to requests (like sending something back to the browser that makes the request).
So our code is: when receiving a request, use the response.writeHead() function to send an HTTP status 200 and content-type of the HTTP header, and use the response.write() function to send text in the corresponding HTTP body "Hello World".
Finally, we call response.end() to complete the response.
For the moment, we don't care about the details of the request, so we are not using the request object.
Where to put the server module
OK, like I promised, we can now go back to how we organize the application. We now have a very basic HTTP server code in the server.js file, and I mentioned that we usually have a file called index.js to call other modules of the application (such as the HTTP server module in server.js) Boot and start the application.
Let's talk about how to turn server.js into a real Node.js module so that it can be used by our (not yet started) index.js main file.
Maybe you've noticed that we've used modules in our code. Like this:
The code copy is as follows:
var http = require("http");
...
http.createServer(...);
Node.js comes with a module called "http". We request it in our code and assign the return value to a local variable.
This turns our local variable into an object with all the public methods provided by the http module.
It is a convention to give this local variable a name that is the same as the module name, but you can also follow your preferences:
The code copy is as follows:
var foo = require("http");
...
foo.createServer(...);
Very good, it is clear how to use the Node.js internal module. How do we create our own module and how do we use it?
Once we turn server.js into a real module, you will understand.
In fact, we don't have to make too many modifications. Turning a piece of code into a module means we need to export the part we want to provide its functionality to the script that requests the module.
At present, the function that our HTTP server needs to export is very simple, because the script requesting the server module only needs to start the server.
We put our server script into a function called start, and we will export this function.
The code copy is as follows:
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
This way, we can now create our main file index.js and start our HTTP in it, although the server's code is still in server.js.
Create an index.js file and write the following:
The code copy is as follows:
var server = require("./server");
server.start();
As you can see, we can use the server module like any other built-in module: request this file and point it to a variable where the exported function can be used by us.
alright. We can now launch our application from our main script, and it's still the same:
The code copy is as follows:
node index.js
Very good, we can now put different parts of our application into different files and connect them together by generating modules.
We still have only the initial part of the entire application: we can receive HTTP requests. But we have to do something - the server should have different reactions to different URL requests.
For a very simple application, you can do this directly in the callback function onRequest(). But as I said, we should add some abstract elements to make our example a little more interesting.
Handling different HTTP requests is a different part in our code, called "routing" - so, let's create a module called routing.
How to make a request "routing"
We want to provide the requested URL and other required GET and POST parameters for the route, and then the route needs to execute the corresponding code based on this data (here the "code" corresponds to the third part of the entire application: a series of real work when receiving the request handler).
Therefore, we need to look at the HTTP request, extract the requested URL from it, and the GET/POST parameter. Should this function belong to a routing or a server (even as a module's own function) be discussed, but it is tentatively the function of our HTTP server.
All the data we need is included in the request object, which is passed as the first parameter of the onRequest() callback function. But to parse this data, we need additional Node.JS modules, which are url and querystring modules, respectively.
The code copy is as follows:
url.parse(string).query
url.parse(string).pathname |
| |
--------------------------------------------------------------------------------------------------------------------------------
http://localhost:8888/start?foo=bar&hello=world
--- ------
| |
querystring(string)["foo"] |
querystring(string)["hello"]
Of course, we can also use the querystring module to parse the parameters in the POST request body, and there will be a demonstration later.
Now let's add some logic to the onRequest() function to find out the URL path requested by the browser:
var http = require("http");
var url = require("url");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
Well, our app can now distinguish different requests by the requested URL path - this allows us to map requests to handlers using routing (not yet completed) to base the URL path on the handler.
In the application we are building, this means that requests from /start and /upload can be handled in different code. We'll see how this content is brought together later.
Now we can write the route, create a file called router.js, and add the following content:
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
As you can see, this code does nothing, but for now it is what it should be. Before adding more logic, let's first look at how to integrate routing and server.
Our servers should know the existence of the route and use it effectively. Of course we can bind this dependency to the server by hard-coded, but programming experience in other languages tells us that this will be a very painful thing, so we will use dependency injection to add routes more loosely Module (you can read Martin Fowlers' masterpiece about dependency injection as background knowledge).
First, let's expand the server's start() function so that the routing function is passed as a parameter:
The code copy is as follows:
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
At the same time, we will extend index.js accordingly so that the routing function can be injected into the server:
The code copy is as follows:
var server = require("./server");
var router = require("./router");
server.start(router.route);
Here, the function we pass does nothing.
If you start the application now (node index.js, always remember this command line) and then request a URL, you will see the application output the corresponding information, which indicates that our HTTP server is already using the routing module and will request it. The path to the route:
The code copy is as follows:
bash$ node index.js
Request for /foo received.
About to route a request for /foo
(The above output has removed the more annoying /favicon.ico request-related parts).
Behavior-driven execution
Please allow me to get out of the topic again and talk about functional programming here.
Passing functions as parameters is not just for technical considerations. For software design, this is actually a philosophical question. Think of this scenario: In the index file, we can pass the router object in, and the server can then call the route function of this object.
Like this, we pass something and the server uses this thing to accomplish something. Hi, that thing called routing, can you help me route this?
But the server doesn't actually need such things. It just needs to finish things. In fact, in order to finish things, you don’t need things at all, what you need is actions. That is, you don't need nouns, you need verbs.
After understanding the most core and basic ideas in this concept, I naturally understood functional programming.
I understood function programming after reading Steve Yegge's masterpiece The Death Penalty in the Kingdom of Nouns. You go and read this book, really. This is one of the books about software that I have given me the joy of reading.
Routing to real request handlers
Back to the topic, our HTTP server and request routing module are now as we expected and can communicate with each other, like a pair of close brothers.
Of course, this is far from enough. Routing, as the name suggests, means that we have different processing methods for different URLs. For example, the "business logic" of processing/start should be different from processing/upload.
With current implementations, the routing process will "end" in the routing module, and the routing module is not a module that really "takes action" against the request, otherwise it will not be very good when our application becomes more complex Expand.
We temporarily call the function that is the routing target a request handler. Now we should not rush to develop routing modules, because if the request handler is not ready, it will not make much sense to improve the routing modules.
Applications require new components, so adding new modules - no longer need to be novel about it. Let's create a module called requestHandlers, and for each request handler, add a placeholder function, and then export these functions as module methods:
The code copy is as follows:
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 this way, we can connect the request handler and the routing module to make the route "has a way to find".
Here we have to make a decision: should we hardcode the requestHandlers module into the route for use, or to add a little more dependency injection? Although like other modes, dependency injection should not be used for use only, in this case, using dependency injection can loosen the coupling between the route and the request handler, and thus make the route more reusable .
This means we have to pass the request handler from the server to the route, but it feels more outrageous to do this. We have to pass the request handler all the way from our main file to the server and then pass it from the server. to the route.
So how do we pass these request handlers? Although we only have 2 handlers now, in a real application, the number of request handlers will continue to increase. Of course, we don’t want to complete the request in the route every time there is a new URL or request handler. Mapping to the handler repeatedly. In addition, there are a lot of if request == x then call handler y in the route, which also makes the system ugly.
Think about it carefully, there are a lot of things, each of which needs to be mapped to a string (that is, the requested URL)? It seems that the associative array can be perfectly competent.
But the result is a bit disappointing, JavaScript does not provide associative arrays -- can also be said to provide them? In fact, in JavaScript, what really provides this kind of functionality is its object.
In this regard, http://msdn.microsoft.com/en-us/magazine/cc163419.aspx has a good introduction, and I'll excerpt here:
In C++ or C#, when we talk about objects, we refer to instances of classes or structures. Objects will have different properties and methods based on the templates they instantiate (that is, the so-called classes). But in JavaScript objects are not this concept. In JavaScript, an object is a collection of key/value pairs--you can think of JavaScript objects as a dictionary with keys as string types.
But if JavaScript objects are just collections of key/value pairs, how can it have methods? Well, the value here can be a string, a number, or... a function!
OK, let's go back to the code at the end. Now we have determined to pass a series of request handlers through an object and need to inject this object into the route() function in a loosely coupled manner.
Let's first introduce this object into the main file index.js:
The code copy is as follows:
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;
server.start(router.route, handle);
虽然handle并不仅仅是一个“东西”(一些请求处理程序的集合),我还是建议以一个动词作为其命名,这样做可以让我们在路由中使用更流畅的表达式,稍后会有说明。
正如所见,将不同的URL映射到相同的请求处理程序上是很容易的:只要在对象中添加一个键为"/"的属性,对应requestHandlers.start即可,这样我们就可以干净简洁地配置/start和/的请求都交由start这一处理程序处理。
在完成了对象的定义后,我们把它作为额外的参数传递给服务器,为此将server.js修改如下:
The code copy is as follows:
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.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
这样我们就在start()函数里添加了handle参数,并且把handle对象作为第一个参数传递给了route()回调函数。
然后我们相应地在route.js文件中修改route()函数:
The code copy is as follows:
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
}
}
exports.route = route;
通过以上代码,我们首先检查给定的路径对应的请求处理程序是否存在,如果存在的话直接调用相应的函数。我们可以用从关联数组中获取元素一样的方式从传递的对象中获取请求处理函数,因此就有了简洁流畅的形如handle[pathname]();的表达式,这个感觉就像在前方中提到的那样:“嗨,请帮我处理了这个路径”。
有了这些,我们就把服务器、路由和请求处理程序在一起了。现在我们启动应用程序并在浏览器中访问http://localhost:8888/start,以下日志可以说明系统调用了正确的请求处理程序:
The code copy is as follows:
Server has started.
Request for /start received.
About to route a request for /start
Request handler 'start' was called.
并且在浏览器中打开http://localhost:8888/可以看到这个请求同样被start请求处理程序处理了:
The code copy is as follows:
Request for / received.
About to route a request for /
Request handler 'start' was called.
让请求处理程序作出响应
very good.不过现在要是请求处理程序能够向浏览器返回一些有意义的信息而并非全是“Hello World”,那就更好了。
这里要记住的是,浏览器发出请求后获得并显示的“Hello World”信息仍是来自于我们server.js文件中的onRequest函数。
其实“处理请求”说白了就是“对请求作出响应”,因此,我们需要让请求处理程序能够像onRequest函数那样可以和浏览器进行“对话”。
不好的实现方式
对于我们这样拥有PHP或者Ruby技术背景的开发者来说,最直截了当的实现方式事实上并不是非常靠谱: 看似有效,实则未必如此。
这里我指的“直截了当的实现方式”意思是:让请求处理程序通过onRequest函数直接返回(return())他们要展示给用户的信息。
我们先就这样去实现,然后再来看为什么这不是一种很好的实现方式。
让我们从让请求处理程序返回需要在浏览器中显示的信息开始。我们需要将requestHandler.js修改为如下形式:
The code copy is as follows:
function start() {
console.log("Request handler 'start' was called.");
return "Hello Start";
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;
OK同样的,请求路由需要将请求处理程序返回给它的信息返回给服务器。因此,我们需要将router.js修改为如下形式:
The code copy is as follows:
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
return handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
return "404 Not found";
}
}
exports.route = route;
正如上述代码所示,当请求无法路由的时候,我们也返回了一些相关的错误信息。
最后,我们需要对我们的server.js进行重构以使得它能够将请求处理程序通过请求路由返回的内容响应给浏览器,如下所示:
The code copy is as follows:
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.");
response.writeHead(200, {"Content-Type": "text/plain"});
var content = route(handle, pathname)
response.write(content);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
如果我们运行重构后的应用,一切都会工作的很好:请求http://localhost:8888/start,浏览器会输出“Hello Start”,请求http://localhost:8888/upload会输出“Hello Upload”,而请求http://localhost:8888/foo 会输出“404 Not found”。
好,那么问题在哪里呢?简单的说就是: 当未来有请求处理程序需要进行非阻塞的操作的时候,我们的应用就“挂”了。
没理解?没关系,下面就来详细解释下。
阻塞与非阻塞
正如此前所提到的,当在请求处理程序中包括非阻塞操作时就会出问题。但是,在说这之前,我们先来看看什么是阻塞操作。
我不想去解释“阻塞”和“非阻塞”的具体含义,我们直接来看,当在请求处理程序中加入阻塞操作时会发生什么。
这里,我们来修改下start请求处理程序,我们让它等待10秒以后再返回“Hello Start”。因为,JavaScript中没有类似sleep()这样的操作,所以这里只能够来点小Hack来模拟实现。
让我们将requestHandlers.js修改成如下形式:
The code copy is as follows:
function start() {
console.log("Request handler 'start' was called.");
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
sleep(10000);
return "Hello Start";
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;
上述代码中,当函数start()被调用的时候,Node.js会先等待10秒,之后才会返回“Hello Start”。当调用upload()的时候,会和此前一样立即返回。
(当然了,这里只是模拟休眠10秒,实际场景中,这样的阻塞操作有很多,比方说一些长时间的计算操作等。)
接下来就让我们来看看,我们的改动带来了哪些变化。
如往常一样,我们先要重启下服务器。为了看到效果,我们要进行一些相对复杂的操作(跟着我一起做): 首先,打开两个浏览器窗口或者标签页。在第一个浏览器窗口的地址栏中输入http://localhost:8888/start, 但是先不要打开它!
在第二个浏览器窗口的地址栏中输入http://localhost:8888/upload, 同样的,先不要打开它!
接下来,做如下操作:在第一个窗口中(“/start”)按下回车,然后快速切换到第二个窗口中(“/upload”)按下回车。
注意,发生了什么: /start URL加载花了10秒,这和我们预期的一样。但是,/upload URL居然也花了10秒,而它在对应的请求处理程序中并没有类似于sleep()这样的操作!
Why is this?原因就是start()包含了阻塞操作。形象的说就是“它阻塞了所有其他的处理工作”。
这显然是个问题,因为Node一向是这样来标榜自己的:“在node中除了代码,所有一切都是并行执行的”。
这句话的意思是说,Node.js可以在不新增额外线程的情况下,依然可以对任务进行并行处理―― Node.js是单线程的。它通过事件轮询(event loop)来实现并行操作,对此,我们应该要充分利用这一点―― 尽可能的避免阻塞操作,取而代之,多使用非阻塞操作。
然而,要用非阻塞操作,我们需要使用回调,通过将函数作为参数传递给其他需要花时间做处理的函数(比方说,休眠10秒,或者查询数据库,又或者是进行大量的计算)。
对于Node.js来说,它是这样处理的:“嘿,probablyExpensiveFunction()(译者注:这里指的就是需要花时间处理的函数),你继续处理你的事情,我(Node.js线程)先不等你了,我继续去处理你后面的代码,请你提供一个callbackFunction(),等你处理完之后我会去调用该回调函数的,谢谢!”
(如果想要了解更多关于事件轮询细节,可以阅读Mixu的博文――理解node.js的事件轮询。)
接下来,我们会介绍一种错误的使用非阻塞操作的方式。
和上次一样,我们通过修改我们的应用来暴露问题。
这次我们还是拿start请求处理程序来“开刀”。将其修改成如下形式:
The code copy is as follows:
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;
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;
上述代码中,我们引入了一个新的Node.js模块,child_process。之所以用它,是为了实现一个既简单又实用的非阻塞操作:exec()。
exec()做了什么呢?它从Node.js来执行一个shell命令。在上述例子中,我们用它来获取当前目录下所有的文件(“ls -lah”),然后,当/startURL请求的时候将文件信息输出到浏览器中。
上述代码是非常直观的: 创建了一个新的变量content(初始值为“empty”),执行“ls -lah”命令,将结果赋值给content,最后将content返回。
和往常一样,我们启动服务器,然后访问“http://localhost:8888/start” 。
之后会载入一个漂亮的web页面,其内容为“empty”。 What's going on?
这个时候,你可能大致已经猜到了,exec()在非阻塞这块发挥了神奇的功效。它其实是个很好的东西,有了它,我们可以执行非常耗时的shell操作而无需迫使我们的应用停下来等待该操作。
(如果想要证明这一点,可以将“ls -lah”换成比如“find /”这样更耗时的操作来效果)。
然而,针对浏览器显示的结果来看,我们并不满意我们的非阻塞操作,对吧?
好,接下来,我们来修正这个问题。在这过程中,让我们先来看看为什么当前的这种方式不起作用。
问题就在于,为了进行非阻塞工作,exec()使用了回调函数。
在我们的例子中,该回调函数就是作为第二个参数传递给exec()的匿名函数:
The code copy is as follows:
function (error, stdout, stderr) {
content = stdout;
}
现在就到了问题根源所在了:我们的代码是同步执行的,这就意味着在调用exec()之后,Node.js会立即执行return content ;在这个时候,content仍然是“empty”,因为传递给exec()的回调函数还未执行到――因为exec()的操作是异步的。
我们这里“ls -lah”的操作其实是非常快的(除非当前目录下有上百万个文件)。这也是为什么回调函数也会很快的执行到―― 不过,不管怎么说它还是异步的。
为了让效果更加明显,我们想象一个更耗时的命令: “find /”,它在我机器上需要执行1分钟左右的时间,然而,尽管在请求处理程序中,我把“ls -lah”换成“find /”,当打开/start URL的时候,依然能够立即获得HTTP响应―― 很明显,当exec()在后台执行的时候,Node.js自身会继续执行后面的代码。并且我们这里假设传递给exec()的回调函数,只会在“find /”命令执行完成之后才会被调用。
那究竟我们要如何才能实现将当前目录下的文件列表显示给用户呢?
好,了解了这种不好的实现方式之后,我们接下来来介绍如何以正确的方式让请求处理程序对浏览器请求作出响应。
以非阻塞操作进行请求响应
我刚刚提到了这样一个短语―― “正确的方式”。而事实上通常“正确的方式”一般都不简单。
不过,用Node.js就有这样一种实现方案: 函数传递。下面就让我们来具体看看如何实现。
到目前为止,我们的应用已经可以通过应用各层之间传递值的方式(请求处理程序-> 请求路由-> 服务器)将请求处理程序返回的内容(请求处理程序最终要显示给用户的内容)传递给HTTP服务器。
现在我们采用如下这种新的实现方式:相对采用将内容传递给服务器的方式,我们这次采用将服务器“传递”给内容的方式。 从实践角度来说,就是将response对象(从服务器的回调函数onRequest()获取)通过请求路由传递给请求处理程序。 随后,处理程序就可以采用该对象上的函数来对请求作出响应。
原理就是如此,接下来让我们来一步步实现这种方案。
先从server.js开始:
The code copy is as follows:
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);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
相对此前从route()函数获取返回值的做法,这次我们将response对象作为第三个参数传递给route()函数,并且,我们将onRequest()处理程序中所有有关response的函数调都移除,因为我们希望这部分工作让route()函数来完成。
下面就来看看我们的router.js:
The code copy is as follows:
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();
}
}
exports.route = route;
同样的模式:相对此前从请求处理程序中获取返回值,这次取而代之的是直接传递response对象。
如果没有对应的请求处理器处理,我们就直接返回“404”错误。
最后,我们将requestHandler.js修改为如下形式:
The code copy is as follows:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
exec("ls -lah", function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
我们的处理程序函数需要接收response参数,为了对请求作出直接的响应。
start处理程序在exec()的匿名回调函数中做请求响应的操作,而upload处理程序仍然是简单的回复“Hello World”,只是这次是使用response对象而已。
这时再次我们启动应用(node index.js),一切都会工作的很好。
如果想要证明/start处理程序中耗时的操作不会阻塞对/upload请求作出立即响应的话,可以将requestHandlers.js修改为如下形式:
The code copy is as follows:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
exec("find /",
{ timeout: 10000, maxBuffer: 20000*1024 },
function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
这样一来,当请求http://localhost:8888/start的时候,会花10秒钟的时间才载入,而当请求http://localhost:8888/upload的时候,会立即响应,纵然这个时候/start响应还在处理中。
更有用的场景
到目前为止,我们做的已经很好了,但是,我们的应用没有实际用途。
服务器,请求路由以及请求处理程序都已经完成了,下面让我们按照此前的用例给网站添加交互:用户选择一个文件,上传该文件,然后在浏览器中看到上传的文件。 为了保持简单,我们假设用户只会上传图片,然后我们应用将该图片显示到浏览器中。
好,下面就一步步来实现,鉴于此前已经对JavaScript原理性技术性的内容做过大量介绍了,这次我们加快点速度。
要实现该功能,分为如下两步: 首先,让我们来看看如何处理POST请求(非文件上传),之后,我们使用Node.js的一个用于文件上传的外部模块。之所以采用这种实现方式有两个理由。
第一,尽管在Node.js中处理基础的POST请求相对比较简单,但在这过程中还是能学到很多。
第二,用Node.js来处理文件上传(multipart POST请求)是比较复杂的,它不在本书的范畴,但,如何使用外部模块却是在本书涉猎内容之内。
处理POST请求
考虑这样一个简单的例子:我们显示一个文本区(textarea)供用户输入内容,然后通过POST请求提交给服务器。最后,服务器接受到请求,通过处理程序将输入的内容展示到浏览器中。
/start请求处理程序用于生成带文本区的表单,因此,我们将requestHandlers.js修改为如下形式:
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" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
好了,现在我们的应用已经很完善了,都可以获得威比奖(Webby Awards)了,哈哈。(译者注:威比奖是由国际数字艺术与科学学院主办的评选全球最佳网站的奖项,具体参见详细说明)通过在浏览器中访问http://localhost:8888/start就可以看到简单的表单了,要记得重启服务器哦!
你可能会说:这种直接将视觉元素放在请求处理程序中的方式太丑陋了。说的没错,但是,我并不想在本书中介绍诸如MVC之类的模式,因为这对于你了解JavaScript或者Node.js环境来说没多大关系。
余下的篇幅,我们来探讨一个更有趣的问题: 当用户提交表单时,触发/upload请求处理程序处理POST请求的问题。
现在,我们已经是新手中的专家了,很自然会想到采用异步回调来实现非阻塞地处理POST请求的数据。
这里采用非阻塞方式处理是明智的,因为POST请求一般都比较“重” ―― 用户可能会输入大量的内容。用阻塞的方式处理大数据量的请求必然会导致用户操作的阻塞。
为了使整个过程非阻塞,Node.js会将POST数据拆分成很多小的数据块,然后通过触发特定的事件,将这些小数据块传递给回调函数。这里的特定的事件有data事件(表示新的小数据块到达了)以及end事件(表示所有的数据都已经接收完毕)。
我们需要告诉Node.js当这些事件触发的时候,回调哪些函数。怎么告诉呢? 我们通过在request对象上注册监听器实现。这里的request对象是每次接收到HTTP请求时候,都会把该对象传递给onRequest回调函数。
As shown below:
The code copy is as follows:
request.addListener("data", function(chunk) {
// called when a new chunk of data was received
});
request.addListener("end", function() {
// called when all chunks of data have been received
});
问题来了,这部分逻辑写在哪里呢? 我们现在只是在服务器中获取到了request对象―― 我们并没有像之前response对象那样,把request 对象传递给请求路由和请求处理程序。
在我看来,获取所有来自请求的数据,然后将这些数据给应用层处理,应该是HTTP服务器要做的事情。因此,我建议,我们直接在服务器中处理POST数据,然后将最终的数据传递给请求路由和请求处理器,让他们来进行进一步的处理。
因此,实现思路就是: 将data和end事件的回调函数直接放在服务器中,在data事件回调中收集所有的POST数据,当接收到所有数据,触发end事件后,其回调函数调用请求路由,并将数据传递给它,然后,请求路由再将该数据传递给请求处理程序。
还等什么,马上来实现。先从server.js开始:
The code copy is as follows:
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.");
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '"+
postDataChunk + "'.");
});
request.addListener("end", function() {
route(handle, pathname, response, postData);
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
上述代码做了三件事情: 首先,我们设置了接收数据的编码格式为UTF-8,然后注册了“data”事件的监听器,用于收集每次接收到的新数据块,并将其赋值给postData 变量,最后,我们将请求路由的调用移到end事件处理程序中,以确保它只会当所有数据接收完毕后才触发,并且只触发一次。我们同时还把POST数据传递给请求路由,因为这些数据,请求处理程序会用到。
上述代码在每个数据块到达的时候输出了日志,这对于最终生产环境来说,是很不好的(数据量可能会很大,还记得吧?),但是,在开发阶段是很有用的,有助于让我们看到发生了什么。
我建议可以尝试下,尝试着去输入一小段文本,以及大段内容,当大段内容的时候,就会发现data事件会触发多次。
再来点酷的。我们接下来在/upload页面,展示用户输入的内容。要实现该功能,我们需要将postData传递给请求处理程序,修改router.js为如下形式:
The code copy is as follows:
function route(handle, pathname, response, postData) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, postData);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
然后,在requestHandlers.js中,我们将数据包含在对upload请求的响应中:
The code copy is as follows:
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" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent: " + postData);
response.end();
}
exports.start = start;
exports.upload = upload;
好了,我们现在可以接收POST数据并在请求处理程序中处理该数据了。
我们最后要做的是: 当前我们是把请求的整个消息体传递给了请求路由和请求处理程序。我们应该只把POST数据中,我们感兴趣的部分传递给请求路由和请求处理程序。在我们这个例子中,我们感兴趣的其实只是text字段。
我们可以使用此前介绍过的querystring模块来实现:
The code copy is as follows:
var querystring = require("querystring");
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" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent the text: "+
querystring.parse(postData).text);
response.end();
}
exports.start = start;
exports.upload = upload;
好了,以上就是关于处理POST数据的全部内容。
处理文件上传
最后,我们来实现我们最终的用例:允许用户上传图片,并将该图片在浏览器中显示出来。
回到90年代,这个用例完全可以满足用于IPO的商业模型了,如今,我们通过它能学到这样两件事情: 如何安装外部Node.js模块,以及如何将它们应用到我们的应用中。
这里我们要用到的外部模块是Felix Geisendörfer开发的node-formidable模块。它对解析上传的文件数据做了很好的抽象。 其实说白了,处理文件上传“就是”处理POST数据―― 但是,麻烦的是在具体的处理细节,所以,这里采用现成的方案更合适点。
使用该模块,首先需要安装该模块。Node.js有它自己的包管理器,叫NPM。它可以让安装Node.js的外部模块变得非常方便。通过如下一条命令就可以完成该模块的安装:
The code copy is as follows:
npm install formidable
如果终端输出如下内容:
The code copy is as follows:
npm info build Success: [email protected]
npm ok
就说明模块已经安装成功了。
现在我们就可以用formidable模块了――使用外部模块与内部模块类似,用require语句将其引入即可:
The code copy is as follows:
var formidable = require("formidable");
这里该模块做的就是将通过HTTP POST请求提交的表单,在Node.js中可以被解析。我们要做的就是创建一个新的IncomingForm,它是对提交表单的抽象表示,之后,就可以用它解析request对象,获取表单中需要的数据字段。
node-formidable官方的例子展示了这两部分是如何融合在一起工作的:
The code copy is as follows:
var formidable = require('formidable'),
http = require('http'),
sys = require('sys');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:/n/n');
res.end(sys.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8888);
如果我们将上述代码,保存到一个文件中,并通过node来执行,就可以进行简单的表单提交了,包括文件上传。然后,可以看到通过调用form.parse传递给回调函数的files对象的内容,如下所示:
The code copy is as follows:
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] } } }
为了实现我们的功能,我们需要将上述代码应用到我们的应用中,另外,我们还要考虑如何将上传文件的内容(保存在/tmp目录中)显示到浏览器中。
我们先来解决后面那个问题: 对于保存在本地硬盘中的文件,如何才能在浏览器中看到呢?
显然,我们需要将该文件读取到我们的服务器中,使用一个叫fs的模块。
我们来添加/showURL的请求处理程序,该处理程序直接硬编码将文件/tmp/test.png内容展示到浏览器中。当然了,首先需要将该图片保存到这个位置才行。
将requestHandlers.js修改为如下形式:
The code copy is as follows:
var querystring = require("querystring"),
fs = require("fs");
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" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent the text: "+
querystring.parse(postData).text);
response.end();
}
function show(response, postData) {
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;
我们还需要将这新的请求处理程序,添加到index.js中的路由映射表中:
The code copy is as follows:
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);
重启服务器之后,通过访问http://localhost:8888/show,就可以看到保存在/tmp/test.png的图片了。
好,最后我们要的就是:
在/start表单中添加一个文件上传元素
将node-formidable整合到我们的upload请求处理程序中,用于将上传的图片保存到/tmp/test.png
将上传的图片内嵌到/uploadURL输出的HTML中
第一项很简单。只需要在HTML表单中,添加一个multipart/form-data的编码类型,移除此前的文本区,添加一个文件上传组件,并将提交按钮的文案改为“Upload file”即可。 如下requestHandler.js所示:
The code copy is as follows:
var querystring = require("querystring"),
fs = require("fs");
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();
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent the text: "+
querystring.parse(postData).text);
response.end();
}
function show(response, postData) {
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;
very good.下一步相对比较复杂。这里有这样一个问题: 我们需要在upload处理程序中对上传的文件进行处理,这样的话,我们就需要将request对象传递给node-formidable的form.parse函数。
但是,我们有的只是response对象和postData数组。看样子,我们只能不得不将request对象从服务器开始一路通过请求路由,再传递给请求处理程序。 或许还有更好的方案,但是,不管怎么说,目前这样做可以满足我们的需求。
到这里,我们可以将postData从服务器以及请求处理程序中移除了―― 一方面,对于我们处理文件上传来说已经不需要了,另外一方面,它甚至可能会引发这样一个问题: 我们已经“消耗”了request对象中的数据,这意味着,对于form.parse来说,当它想要获取数据的时候就什么也获取不到了。(因为Node.js不会对数据做缓存)
我们从server.js开始―― 移除对postData的处理以及request.setEncoding (这部分node-formidable自身会处理),转而采用将request对象传递给请求路由的方式:
The code copy is as follows:
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;
接下来是router.js ―― 我们不再需要传递postData了,这次要传递request对象:
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;
现在,request对象就可以在我们的upload请求处理程序中使用了。node-formidable会处理将上传的文件保存到本地/tmp目录中,而我们需要做的是确保该文件保存成/tmp/test.png。 没错,我们保持简单,并假设只允许上传PNG图片。
这里采用fs.renameSync(path1,path2)来实现。要注意的是,正如其名,该方法是同步执行的, 也就是说,如果该重命名的操作很耗时的话会阻塞。 这块我们先不考虑。
接下来,我们把处理文件上传以及重命名的操作放到一起,如下requestHandlers.js所示:
The code copy is as follows:
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");
fs.renameSync(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;
好了,重启服务器,我们应用所有的功能就可以用了。选择一张本地图片,将其上传到服务器,然后浏览器就会显示该图片。
Summary and prospect
恭喜,我们的任务已经完成了!我们开发完了一个Node.js的web应用,应用虽小,但却“五脏俱全”。 期间,我们介绍了很多技术点:服务端JavaScript、函数式编程、阻塞与非阻塞、回调、事件、内部和外部模块等等。
当然了,还有许多本书没有介绍到的: 如何操作数据库、如何进行单元测试、如何开发Node.js的外部模块以及一些简单的诸如如何获取GET请求之类的方法。
但本书毕竟只是一本给初学者的教程―― 不可能覆盖到所有的内容。
幸运的是,Node.js社区非常活跃(作个不恰当的比喻就是犹如一群有多动症小孩子在一起,能不活跃吗?), 这意味着,有许多关于Node.js的资源,有什么问题都可以向社区寻求解答。 其中Node.js社区的wiki以及NodeCloud就是最好的资源。