Nodejs Introductory note (i)

Source: Internet
Author: User
Tags php multithreading

node leaves the "HTTP Server" layer out and targets the browser user directly

If PHP is to be configured with a powerful and complex HTTP server, such as Apache, IIS or Nginx, you also need to configure PHP as a module of the HTTP server, or invoke the PHP interpreter using the FASTCGI protocol. This architecture is the way the browser-http server-php interpreter is organized

Node's biggest feature is asynchronous I/O, which is known as a single thread, and is not blocked, as is the case with PHP multithreading, for example, when a thread encounters an I/O operation, does not wait for I/O operations to complete or data to return, but simply sends I/O requests to the operating system. And in the continuation of the next sentence. Conversely, multithreading is blocking, blocking what to do? Open one more thread to do the bad? (Please self-Baidu) the following two pictures more clearly understand

Multithreading:

Single Thread

Here's what we're going to learn tonight.

varHTTP = require ("http");//Creating an HTTP modulehttp.createserver (function (req,res) {Res.writehead ( $,{'Content-type':'text/html'}); Res.write (""); Res.end ("<p>node</P>");}). Listen ( the);//Creating PortsConsole.log ("Run successfully")

Getting started writing rotten statements is creating node's core module HTTP

Creating a template node provides exports and require two objects.

Exports is a public excuse for modules require is to get the interface usage of the module is very simple

 // module.js    Name;exports.tagname  = function (a) { Name  = A;} exports.tagtable  = function () {Console.log (  " i am   +name)}  
// GetModule var Get = require ("./module"); get. tagname ("LEE"get. tagtable ();

As shown in the output is Lee

But this is all one-way loaded, for example

 var  get  = require ( " ./module  "  ); get . tagname ( " lee   "  var  get  = require ( " ./module   "  get . tagname ( " lee2   "  get . tagtable (); 

The output is LEE2 because it's all pointing to the same one, and the second one overrides the first one, so it's LEE2.

Sometimes writing the code is a little bit like this.

function Hello () {    var  name;      this. SetName = function (thyname) {         = thyname;     };      this. SayHello = function () {         console.log (' + Name );     
var Hello = require ('./hello'new  hello (); Hello.setname ('  Lee'

The output of "Lee";

Summing up what I learned tonight, I have a clearer understanding of the module

Continue to grow in ....

  

  

Nodejs Introductory note (i)

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.