Play to node. JS (ii)

Source: Internet
Author: User

Play to node. JS (ii)

  first look at the last content, The last time we used the Hello World program that introduced node. JS and wrote the first server side, in this Hello World program, request the own HTTP module and assign it to the HTTP variable, and then invoke the Createserver function of the HTTP module , this function returns an object that has a method called Listen, and this method has a numeric parameter that specifies the port number that the HTTP server listens on, which we defined as the 8888 port number. So why does it look a little complicated? That's because we passed an anonymous function to the Createserver function. In fact, the following code can also accomplish the same purpose:

1var http = require ("http");2 3 function ONrequest (request, response) {4Response.writehead (200, {"Content-type":"Text/plain"});5Response.Write ("Happy women ' s Day");6 Response.End ();7 }8 9Http.createserver (ONrequest). Listen (8888);

Let 's go through the details of how the server handles the request.

How the server handles the request

  OK, so let's take a quick look at the rest of our server code, the main part of our callback function onrequest () .

When the callback starts and our onrequest () function is triggered, two parameters are passed in:request and response .

They are objects, and you can use their methods to handle the details of HTTP requests and respond to requests (such as sending something back to the requesting browser).

So our code is: When the request is received, use the response.writehead () function to send a content type of HTTP status 200 and HTTP header (Content-type), using Response.Write () The function sends the text "Hello World" in the appropriate body of HTTP.

Finally, we call Response.End () to complete the response.

For now, we don't care about the details of the request, so we're not using the requests object.

Modularity of the program

When we first approached the program, if we need to write a larger project, there will always be someone to tell us to write code, easy to understand, then Nodejs, how should it be written? Should we write all the code in a file? And how do you organize the code? What if I wanted to make the code easy to extend so that I could add some new features that are less cumbersome and want to keep the code readable?

In fact, we just need to put the code according to the function to put in different templates, it is not difficult to maintain the separation of code. This method allows you to have a master file that you can execute with Nodejs, and you can have other feature templates that can be called by the main file and other modules.

In fact, we have to turn the last program into a module just to export the functionality we want to provide to the script that requested the module, we put the server script into a function called start, then we will export the function, create a server.js file and write the following code.

1var http = require ("http");2 3 function Start () {4 function ONrequest (request, response) {5Console.log ("Request received.");6Response.writehead (200, {"Content-type":"Text/plain"});7Response.Write ("Happy Women ' s Day");8 Response.End ();9   }Ten  OneHttp.createserver (ONrequest). Listen (8888); AConsole.log ("Server has started."); - } -  theExports.start = start;

  In this way, we can now create our main file index.js and launch our HTTP in it, although the server's code is still in server.js .

Create the index.js file and write the following:

1 var server = require ("./server"); 2 3 server.start ();

 As you can see, we could use the server module just like any other built-in module: request this file and point it to a variable, where the exported function can be used by us.

All right. We can now launch our app from our main script, and it's still the same way:

1 Node index.js

  Very well, we can now put different parts of our application into different files, and connect them together by building a module.

We still only have the initial part of the entire application: we can receive HTTP requests. But we have to do something-the server should react differently 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 bit more interesting.

But that will be the next time, and this is the end of the content.

PS: This blog Welcome to forward, but please specify the blog address and author, because I level limited, if there is wrong, welcome point, Thank you ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.net

<. ))) ≦

Play to node. JS (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.