NodeJs Getting Started---1

Source: Internet
Author: User
Tags node server

1. Analyzing the HTTP Server

var http = require ("http"); Http.createserver (function (request, response) {  Response.writehead (200, {") Content-type ":" Text/plain "});  Response.Write ("Hello World");  Response.End ();}). Listen (8888);

The first line requests (require)node. JS's own HTTP module and assigns it to the HTTP variable.

Next we invoke the function provided by the HTTP module: createserver . This function returns an object that has a method called Listen , which has a numeric parameter that specifies the port number that the HTTP server listens on.

2. Event-driven callbacks

Nodejs Event-driven

When we use the Http.createserver method, of course we don't just want a server that listens on a port, we also want it to do something when the server receives an HTTP request.

The problem is that this is asynchronous: The request can arrive at any time, but our server is running in a single process.

When writing PHP applications, we are not worried at all: whenever a request comes in, the Web server (usually Apache) creates a new process for the request and starts executing the appropriate PHP script from start to finish.

So in our node. JS program, when a new request arrives at Port 8888, how do we control the process?

Well, that's where Node.js/javascript's event-driven design can really help-although we have to learn some new concepts to master it. Let's take a look at how these concepts are applied in our server code.

We created the server and passed a function to the method that created it. Whenever our server receives a request, the function is called.

We don't know when this is going to happen, but we now have a place to deal with the request: It's the function we passed in the past. It does not matter whether it is a pre-defined function or an anonymous function.

This is the legendary callback. We pass a function to a method that calls this function to make a callback when a corresponding event occurs.

var http = require ("http"), function onrequest (request, Response) {  Console.log ("callback function Execution");  Response.writehead ($, {"Content-type": "Text/plain"});  Response.Write ("Hello World");  Response.End ();} Http.createserver (ONrequest). Listen (8888); Console.log ("Server started working");

When you run node Server.js, the server starts working immediately, and when we send a request (access localhost:8888) to the server, it outputs "callback function execution", which is the event-driven asynchronous server-side JavaScript and its callbacks!

(Note that our server may output two "callback function execution" When we access the Web page on the server.) That's because most servers will try to read Http://localhost:8888/favicon.ico when you visit http://localhost:8888/

3. How the server handles requests

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 ( ) function to send the text "Hello World" in the appropriate body of HTTP.

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

4. The service-side module should be there.

Let's talk about how to turn server.js into a real node. js module so that it can be used by our index.js Master file (which is not working).

node. JS uses the exports and require objects to solve the problem of providing interfaces and referencing modules externally.

We put our server script in a function called start , and then we export the function.

var http = require ("http"), function Start () {  function onrequest (request, Response) {    Console.log ("callback function Execution");    Response.writehead ($, {"Content-type": "Text/plain"});    Response.Write ("Hello World");    Response.End ();  }  Http.createserver (ONrequest). Listen (8888);  Console.log ("Start");} Exports.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:

var server = require ("./server"); Server.start ();

  Note:index.js and Server.js are placed in the same directory, although "./server" says so,

Exports.start = start; interface
var server = require ("./server"); Introduced

NodeJs Getting Started---1

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.