A Preliminary Study on node. js-surpassing yesterday's series (3)

Source: Internet
Author: User
Surpassing yesterday's series (3)

In fact, learning is always passive. One day, when we understand that we need to study hard, we will find that the world of knowledge is so mighty that we cannot see the bank, we can't even see the sunrise, so it's difficult to get confused.Therefore, progress is never easy.

------------------------------------------

Node. js contacts and learns new things. It is always good for you in the future.

Start from here: http://www.nodejs.org/

 

1, Require Problem As follows:Code:
 
HTTP = require ("HTTP ");
HTTP is imported as a module so that you can use it. Similar to the import in Java. Javascript itself does not support module, so the so-called Commonjs

The emergence of the commonjs (http://www.commonjs.org) specification aims to build an ecosystem of Javascript in terms of web servers, desktops, command line tools, and browsers.

Commonjs has developed some specifications to solve these problems, and node. JS is an implementation of these specifications. Node. js implements the require method as a method for introducing modules. Meanwhile, NPM implements dependency management, automatic module installation, and other functions based on the package specification defined by commonjs.

The basic syntax is as follows:

1. modulea. js

 

 
Module. Exports. Add =FunctionAdd (n, m ){VaRCount = N +M; console. Log (n+ "+" + M + "=" +Count );ReturnCount ;};

2.Moduleb. js

 
A = require ("./modulea"); A. Add (1, 2 );

 

For more specific examples, seeArticle:

Http://openmymind.net/2012/2/3/Node-Require-and-Exports/

 

2. About callback Functions

Function callback is widely used in node. js encoding. Familiarize yourself with the following:

Instance 1
  1. FunctionInvoke_and_add (a, B ){
  2. ReturnA () + B ();
  3. }
  4. FunctionOne (){
  5. Return1;
  6. }
  7. FunctionTwo (){
  8. Return2;
  9. }
  10. Invoke_and_add (ONE, TWO );

The result is 3;

 

Example 2 anonymous Functions
    1. invoke_and_add ( function () { return 1 ;}, function () { return 2 ;})

we replaced one and two functions with anonymous functions.

Through the above two instances, the callback function is defined as: passing a function a to another function B, and B executes function. Function A is called a callback function. If there is no name, it is called an anonymous callback function.

 

For the relationship between callback and Asynchronization, refer to this article:

Http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html

 

3, hello World

Use node. js to build a server, which is easy to understand and mentioned in the Getting Started article.

VaRHTTP = require ("HTTP"); Http. createserver (Function(Request, response) {response. writehead (200, {"Content-Type": "text/plain"}); Response. Write ("Hello World"); Response. End () ;}). Listen (8888 );

Listen to port 8888, and any request will print Hello world.

 

4. Implement an API to call the Baidu Map

Through coordinates, the specific request address is to send an HTTP request without even returning the resolution value.

 HTTP = Require ("HTTP" );  VaR Key = "37109c0ee6f924cb5e934fa08c6b1676" ;  VaR Location = "39.983424, % 20116.322987" ;  VaR Output = "JSON" ;  VaR Path = "/geocoder? Location = "+ location +" & Output = "+ output +" & Key = "+ Key  VaR Resultjson = "" ; Options = {Host: "Api.map.baidu.com" , Method: "Get" , Path: path };  // Http://api.map.baidu.com/geocoder? Output = JSON & location = 39.983424, % 20116.322987 & Key = 37109c0ee6f924cb5e934fa08c6b1676  //  Http://api.map.baidu.com/geocoder? Location = latitude, longitude & Output = output format type & Key = User Key Req = http. Request (options, Function  (RES ){  //  Console. Log ('status: '+ res. statuscode );      //  Console. Log ('headers: '+ JSON. stringify (res. headers ));      //  Retrieve body content Res. On ('data ',Function  (Chunk) {console. Log ( 'Body: '+ Chunk); resultjson = Resultjson + Chunk;}); console. Log ( 'Resultjson: '+ Resultjson) ;}); Req. On ( 'Error ', Function  (E) {console. Log ( 'Problem with request: '+ E. Message) ;}); Req. End ();  //  Returned results:  /* ** Resultjson: Body: {"status": "OK", "result": {"location": {"LNG": 116.322987, "Lat": 39.983424 }, "formatted_address": "room 1101-08, No. 27 Zhongguancun Street, Haidian District, Beijing", "business": "Renmin University, Zhongguancun, Suzhou Street", "addresscomponent": {"city ": "Beijing", "District": "Haidian District", "Province": "Beijing", "street": "Zhongguancun Street", "street_number ": "Room 1101-08, No. 27"}, "citycode": 131 }}*  */ 

 

 

--------------------------------------------------

Let's move on!

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.