node.js study notes (d) core modules

We know that there are two types of node modules: one is the core module (system module), a file module (custom module or third-party modules) The following briefly node inside those core modules.

First, the difference between the core module and file module

The core module consists of module Node.js Platform, also known as system module.

The core module import rules: (.) ​​Is not a two-dot (..) or the start point and the identifier of the oblique rod (/).

var fs=require("fs")

 

File module: (.) ​​Import rules to a two-dot (..) and the point or slash (/) start identifier

var hi =require("./hi")

Note: We usually import module will be stored in node_modules folder.

Two, os (operating system) kernel module

os Module provides the operating system-related use

Detailed NodeJs official Chinese documents address os module methods: http://nodejs.cn/api/os.html , here I simply talk

os.js

the require OS = const ( 'OS' );
 // os.hostname () method returns the name of the host operating system as a string. 
console.log ( 'host name' + os.hostname ());
 // os.cpus () method returns an array of objects containing information for each logical CPU core. 
console.log ( 'native the CPU' + os.cpus ());
 // operating system platform os.platform () method returns a string that specifies Node.js compile time. 
console.log ( 'native operating system platform' + os.platform ());
 // os.type () method returns a string, returns the name of the operating system. 
console.log ( 'native operating system:' + os.type ())

run:

 

 Three: path (path) core modules

path Module provides a tool for processing the file path and directory path used.

For more modular approach path of NodeJs official Chinese documents Address: http://nodejs.cn/api/path.html

path.js

the require path = const ( 'path' );
 var testData = 'E: /travel/index.html' ;
 // path.basename () method returns the last part of the path, 
the console.log (path.basename (testData));
 // path.dirname () method returns the name of the directory path, i.e. removing the last part of the path of all 
the console.log (path.dirname (testData));
 // path.extname () method returns the path of extension, from the last appears. (period) character string to the end of the last part of the path. 
console.log (path.extname (testData));

run

 

 Four, URL core module

url Module for processing and analysis URL.

Detailed NodeJs official Chinese document modular approach url address: http://nodejs.cn/api/url.html

url.js

the require URL = const ( 'URL' );
 var Data = 'HTTP:? = //itcat.cn name Age = 22 is John Doe &' ; 

the console.log ( 'second default parameter False -' , url.parse (Data )); 
the console.log ( 'the second parameter to true -', url.parse (Data, to true )); 
the console.log ( 'the second parameter to true -', url.parse (Data, to true ) .query) ;

run

 

 Five, fs (file operation) core modules

fs Module provides an API, is used to mimic the function of the POSIX standard way to interact with the file system.

Detailed NodeJs official Chinese documents address methods modules fs: http://nodejs.cn/api/fs.html

About fs I have a simple application before, and now say again in this or next

fs.js, write data to the a.txt

var FS = the require ( 'FS' ); 
fs.writeFile ( './data/a.txt', 'Happy birthday country seventy, Happy Day everyone', function (ERR) {
     IF (ERR) { 
        the console.log ( 'write failed " );
         return ; 
    } the else { 
        the console.log ( ' write success' ); 
    } 
    
});

run

 

 

 

 

Six, http core module

http module provides an API, for use HTTP server and client

Detailed NodeJs official Chinese documents address methods modules fs: http://nodejs.cn/api/http.html

http.js

// 1. Load the core module http 
var http = the require ( 'http' ); 

// 2. Create a Web server http.createServer () method 
//     returns a Server instance 
var Server = http.createServer (); 

// 3. registration request request event 
//     when a client requests come, it will automatically trigger server request request event, and then perform the second argument: the callback handler 
server.on ( 'request', function (REQ, RES) { 
  Console .log ( 'receiving the client's request, the request address is:' + req.url); 
  the console.log ( 'received a request from a client, a request header address information (objects) are:' + req.headers); 
  Console. log ( 'client receives the request, the request header address information (array) is:' + req.rawHeaders); 
  the console.log ( 'received a request from a client, HTTP version request address is:' +req.httpVersion); 
  the console.log ( 'receiving the client's request, the request is:' + req.method); 
  the console.log ( 'I am requesting client address:' , req.socket.remoteAddress, REQ. socket.remotePort)
   // send a response content (res), res.setHeader () response data to solve distortion 
  res.setHeader ( 'the content-the Type', 'text / HTML; charset = UTF-. 8' );
   // The different url in response to different data 
  var URL = req.url;
   IF (URL === '/' ) { 
    res.end ( "Baidu, you know, <a href='http://www.baidu.com'> Baidu </a> " ); 
  } the else  IF (URL === '/ Login' ) { 
    res.end ( 'the this IS Login Pages')
  }else if(URL === '/ Products' ) {
     var Products = [{ 
        name: 'apple X-' , 
        . price: 8888 
      }, 
      { 
        name: 'Pineapple X-' , 
        . price: 5000 
      }, 
      { 
        name: 'peppers X-' , 
        . price: 1999 
      } 
    ] 
    // NOTE: in response to the contents of only binary data or string 
    // Therefore numbers, objects, arrays, and other required Boolean transformed. 
    res.end (the JSON.stringify (Products)) 
  } the else {
      // res.end () End response data 
     res.end ( '404 Not Found.') 
  } 
  
}) 

// 4. bound port number, the server starts 
server.listen (3000, function () { 
  the console.log ( 'server starts successfully, can be accessed by http://127.0.0.1:3000/ ' ); 
})

Run, and visit the website

 

 Renderings do not show Ha

 In addition, nodejs inside the core module more than that, as the other documents we can go to the official website NodejsAPI View: http://nodejs.cn/api/

Guess you like

Origin www.cnblogs.com/smile-xin/p/11614719.html