NODEJS Create a Web server Hello World program _javascript Tips

Source: Internet
Author: User
Tags call back event listener

The most important application of Node.js is the server order.

One of the main objectives of the design node.js is to provide a highly scalable server environment. This is the difference between the node and the V8 engine that we introduced at the beginning of this chapter. In addition to using the V8 engine to parse JavaScript, Node provides a highly optimized application library to improve server efficiency. For example, HTTP modules are rewritten for fast non-blocking HTTP servers with C. Let's take a look at node's "Hello World" classic example of HTTP server

Copy Code code as follows:

var http=require (' http ');//Introducing the core HTTP module

/* Create Server/*

var server=http.createserver (function (req,res) {
  res.writehead (200,{"content-type": ' text/html '});
  Res.end (' Hello World ');
};

/* Monitor IP and Port * *

Server.listen (8124, ' 127.0.0.1 ', function () {
  console.log (' Server created as successful: 127.0.0.1:8124 ');
};

Run the code as shown below

Zxd@zxd-pc/k/work/learn/nodejs/cvs01
$ node app

Server created as successful: 127.0.0.1:8124

This sample code first includes the HTTP library in the program through the Require method. There are many languages that have a way of including other libraries, and Node uses the COMMONJS module style.

What you need to know now is that the HTTP library has functions that have been assigned to the HTTP object.

Next, we need an HTTP server. Other languages, such as PHP, need to run on servers like Apache, and node differs from them because node itself is the Web server.

But it also means that we need to create the server first. The next line of code calls a Factory mode method of the HTTP module
(Createserver) to create a new HTTP server. The newly created HTTP server is not assigned to any variable, and it will only become an anonymous object that is alive in the global scope.

We can initialize the server through a chained call and tell it to listen on port 8124. When calling Createserver, we passed an anonymous function as an argument. This function binds to the request event processing on the event listener of the newly created server. Message events are the core of JavaScript and node. In this example, whenever a new access request arrives at the Web server, it will invoke the function method we specify to handle it. We call this kind of method a callback (callback). Because whenever an event occurs, we will call back all functions that listen to this event.

Example two:

As you run to another city with no computers on hand, the DOM framework cannot be released as scheduled to learn something new. The most urgent need for this period is to seek a super lightweight rear end to frame my frame, so the claw reaches the legendary server-side javascrpt. Back-end JS is arguably the most famous of Ryan Dahl's Node.js, and the other is jaxer Aptana IDE providers.

First download Node.js, then extract to E disk, renamed node, and then Start menu input cmd, cd command to switch to the Nodejs of the Extract directory:

First example: Hello World.

Create the Hello.js file in the node directory and enter it inside:

var sys = require ("sys"); 

We then enter the command node Hello.js in the name table and we can see the name console output, Hello World.

Second example: Hello world2.

Okay, this time we're trying to output Hello World from the viewer. Create the Http.js in the node directory and enter:

var sys = require ("sys"), 
  http = require ("http"); 
Http.createserver (function (request, response) { 
  Response.sendheader ({"Content-type": "Text/html"}); 
  Response.Write ("Hello world!"); 
  Response.close (); 
}). Listen (8080); 

Then we enter the command node Http.js in the name console and enter http://localhost:8080/in the browser

A third example: Hello World2.

Node.js provides a buffer class for converting strings of different encodings.

There are currently three types of support: ' ASCII ', ' UTF8 ' and ' binary '. For more details here

var buffer = require (' buffer '). Buffer, 
buf = new buffer (256), 
len = Buf.write (' \u00bd + \U00BC = \u00be ', 0); 

Fourth Example: Hello world3.

Synopsis.js 
//synopsis Summary, synopsis, outline 
var http = require (' http '); 
  
Http.createserver (function (request, response) { 
 response.writehead ({' Content-type ': ' Text/plain '}); 
 Response.End (' Hello world\n '); 
Listen (8124); 
  

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.