This article explains the api development manual of nodejs0.8.8. node. js URL Overview (synopsis) is a simple example of a webserver written with node to output & quot; helloworld & quot;: [javascript] & lt; SPANstyle & quot; FONT-SIZE: 18px; FONT-FAMILY: Fa
This article is an explanation of the api development manual for nodejs0.8.8.
Overview (synopsis)
A simple example of a web server written with node is used to output "hello world ":
[Javascript]
Var http = require ('http ');
Http. createServer (function (request, response ){
Response. writeHead (200, {'content-type': 'text/plain '});
Response. end ('Hello World \ n ');
}). Listen (8124 );
Console. log ('server running at http: // 127.0.0.1: 8124 /');
Var http = require ('http ');
Http. createServer (function (request, response ){
Response. writeHead (200, {'content-type': 'text/plain '});
Response. end ('Hello World \ n ');
}). Listen (8124 );
Console. log ('server running at http: // 127.0.0.1: 8124/'); to run this service, you need to put this code into a example. js file, and then use the following code to execute it. in windows, run cmd to enter example. js directory execution
[Javascript]
> Node example. js
Server running at http: // 127.0.0.1: 8124/
> Node example. js
Server running at http: // 127.0.0.1: 8124/the examples in this document can be simply run.
Global objects)
These objects are visible in all modules. Some of them are not really global objects, but module scopes. These objects will be labeled.
Global: object type, global namespace object
In the browser, the highest level of scope is global scope, which means that if you use var to define a variable in global scope, this variable will be defined as global scope. but it is different in node. The highest level scope is not global scope. In a module, use var to define a variable. This variable is only in the scope of this module.
Process: Object Type
For process objects, see the process section.
Console: Object Type
It is usually used to print to stdout and stderr. For details, see the stdio chapter.
Calss: Buffer: function Type
It is often used to process binary data. For details, see the buffer section.
Require (): Method Type
To obtain the required modules, see the Modules section. require is not a real global object scope, but is built into every module.
Require. resolve ()
The built-in require () mechanism is used to query the local module. However, if this module is not loaded, only the file name from resolved is returned.
Require. cache: Object Type
After the module is loaded by the require () method, It will be cached in the cache object. By deleting a key value, you can re-load this module by the next require () method.
Require. extensions: array type
Notify the require method how to handle the specified file extension. See the example.
The extension with the extension suffix sjs is treated as the integrated js file: [javascript] view plaincopyprint?
Require. extensions ['. sjs'] = require. extensions ['. js'];
Require. extensions ['. sjs'] = require. extensions ['. js']; compile your own extension processing: [javascript] view plaincopyprint?
Require. extensions ['. sjs'] = function (module, filename ){
Var content = fs. readFileSync (filename, 'utf8 ');
// Parse the file content and give to module. exports
Module. exports = content;
};
Require. extensions ['. sjs'] = function (module, filename ){
Var content = fs. readFileSync (filename, 'utf8 ');
// Parse the file content and give to module. exports
Module. exports = content;
}; _ Filename: String object
Indicates the name of the file where the code is currently being executed. is the absolute path of the current code file. for a main program, using the same file name in the command line is not unnecessary. in a module, the value of _ filename is the path of the module File.
Example: Run node example. js in/user/mjr: [javascript] view plaincopyprint?
Console. log (_ filename );
/// Users/mjr/example. js
Console. log (_ filename );
/// Users/mjr/example. js_filename is not a real global object scope, but is built into each module.
_ Dirname: string type
Directory where the script is currently executed,
Example: Run node example. js in/user/mjr: [javascript] view plaincopyprint?
Console. log (_ dirname );
/// Users/mjr
Console. log (_ dirname );
/// Users/mjr_dirname is not a real global object scope, but is built into every module.
Module: Object Type
Indicates an index of the current module. In particular, module. exports and exports are the same object. module is not a real global object scope, but is built into each module. For details, see module sysem.
Exports
Share an exclusive module with all current module instances, including the module obtained by require. module. exports and exports are the same object. exports is not a real global object scope, but is built into every module. for details, see module system and module.
SetTimeOut (cb, MS)
When the callback function cb is run in milliseconds, the actual delay depends on external factors, such as the interval granularity of the operating system timer and system loading.
The range of ms settings must be 1 ~ Between 2147483647, if the timeout time exceeds the range, it will be set to 1 ms. In other words, a timer cannot exceed 24.8 days.
Returns an opaque value representing the timer.
ClearTimeOut (t)
Stop a previously created timer with setTimeOut (). The previously set callback function will not be executed.
SetInterval (cb, MS)
Every milliseconds, the callback function cb is executed repeatedly. Note that the internval may actually change depending on external factors, such as the interval granularity of the operating system timer and system loading. it will never be executed in milliseconds, but it may be longer than ms.
Ms. The value range must be 1 ~ Between 2147483647, if the timeout time exceeds the range, it will be set to 1 ms. In other words, a timer cannot exceed 24.8 days.
Returns an opaque value representing the timer.
ClearInterval (t)
Stop a previously created timer with setInterval (). The previously set callback function will not be executed.
The above timer function is a global variable. For details, see timers.