node. JS Core Modules:
1.NODEJS global variable is globals, not window
2.process Process Object
3.console.log () Console output
Built-in Modules
1.util//function is unknown, seemingly inherited sub,base these two classes of
(. Eg) 1 util.inherits (Sub, Base);
2.events//function unknown
3.fs//file stream?
(. Eg)
1 var fs = require ("FS"2 fs.readfile ("file.txt", "Utf-8", readfilecallback);
4.HTTP Server and Client
http. Server events
http. Severrequest\http. Severrespnse
1 var http = require ("http"); 2 3 function Start (req, res) {4 res.writehead ($, {"Contnet-type": "Text/html"}); 5 Res.write ("); 6 Res.end ("<p>the end</p>")7}89 Http.createserver (Start). Listen (3000);
Modules and Packages
1. Create a module
2.Express Framework (MVC)
------------------------------------------------------------Gorgeous split line ah ah ah ~~~~~---------------------------------- ----------------------------------
Learning source:http://www.open-open.com/lib/view/1392611872538 (This section is relatively brief)
Module: Require, exports, module (key words)
Require: Used to load and use other modules in the current module, passing in a module name and returning a module export object. The module name can use a relative path (beginning with ./ ), or an absolute path (beginning with a drive letter such as / or C: ). In addition, the . js extension in the module name can be omitted. Here is an example.
var // relative Path var // JS can be omitted var // Absolute Path // comparetively We also can require json,but with behind-address. JSON var json = require ("./j.json");
Exports: The exports object is an export object of the current module that is used to export the module's public methods and properties. When other modules use the current module through the require function, the exports object of the current module is obtained. In the following example, a public method is exported.
function () { console.log ("Hello world!" );}
Module: Some information about the current module can be accessed through the Module object, but the most useful is to replace the exported object of the current module. For example, the module export object is a normal object by default and can be used in the following way if you want to change to a function.
function () { console.log (' Hello world! ' );};
In the above code, the module default export object is replaced with a function.
1. Module initialization
The JS code in the module is executed only once when the module is used, and the JS export object Exports is initialized during execution, and then reused using the object
Main module: The module that is passed to Nodejs by command-line arguments to start the program is called the main module. The main module is responsible for scheduling other modules that compose the entire program to complete the work. For example, when you start a program with the following command, it main.js is the main module.
var counter1 = require ('./util/counter '); var Counter2 = require ('./util/counter '); // ... some other operation
Counter.js There is a private variable, a public access method.
var i = 0; function count () { i++ = count;
Called in Main
Console.log (Counter1.count ()); Console.log (Counter2.count ()); // Output 1, 2
Part1 Summary:
1. The essence of installing Nodejs is to copy the Nodejs execution program to a directory, and this directory is under the system PATH environment variable so that the terminal can use node to execute
2. Using the CMD module system, the main module acts as a program entry, and each module is initialized only once during execution.
node. JS Learning Note (1)/9.26