node. js API

Source: Internet
Author: User
Tags emit

node. JS v4.4.7 Documentation (Official document)

Buffer

Prior TypedArray to the introduction of ECMAScript (ES6), the JavaScript language had no mechanism for reading or Manip ulating streams of binary data (binary). The class is introduced as part of the Buffer node. js API to make it possible to interact with octet streams in the Contex T of things like TCP streams and file system operations.

Url

This module has a utilities for URL resolution (URL resolution) and parsing. Call require(‘url‘) -to-use it.

URL (global resource Locator (Uniform Resource Locator)) URL parsing

Url.parse () returns the object

parsed URL Objects has some or all of the following fields, depending on whether or not they exist in the URL St Ring. Any parts that is not in the URL string is not being in the parsed object.

URL {
Protocol: ' http: ',//protocol
Slashes:true,//whether there is a slash
Auth:null,
Host: ' jingyan.baidu.com ',//hostname including port information (including port)
Port:null,//Port
Hostname: ' jingyan.baidu.com ',//host Domain
Hash:null,//hash, anchor
Search:null,
Query:null,
Pathname: '/article/93f9803fd3a4dde0e46f55f5.html ',//
Path: '/article/93f9803fd3a4dde0e46f55f5.html ',//concatenation ofpathnameandsearch
HREF: ' http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html '//The full URL

}

Url.format (Urlobj)

Take a parsed URL object, and return a formatted URL string.

Returns a URL string

Url.resolve (from, to)
url. Resolve ( '/one/two/three '  Four ' ) //'/one/two/four ' URL resolve ( ' http://example.com/'  '/one ' ) //'/http Example.com/one ' Url. Resolve ( ' http://example.com/one '   '/two ' ) //' Http://example.com/two ' 
Url.parse (urlstr[, parsequerystring][, Slashesdenotehost])

Take a URL of string, and return an object.

Pass as the true second argument to also parse the query string using querystring the module (node. JS built-in query string module ) . If then the property is always being true query assigned an object, and the property is always search a (poss ibly empty) string. If then the property is not being false query parsed or decoded. Defaults to false .

Query String

This module provides utilities -dealing with query strings (query string processing)

Querystring.stringify (obj[, sep][, eq][, Options])

Serialize An object to a query string. Serializes an object into a query string

Optionally override the default separator ( ‘&‘ ) and Assignment ( ‘=‘ ) characters ( the delimiter is & =).

Querystring.parse (str[, sep][, eq][, Options])

Deserialize (deserialization) a query string to an object. Optionally override the default separator ( ‘&‘ ) and Assignment ( ‘=‘ ) characters.

Querystring.escape Querystring.unescape

Querystring.escape Escape

Querystring.unescape Reversal Righteousness

File System

File I/O is provided-wrappers around standard POSIX functions. To use the This module does require(‘fs‘) . all the methods has asynchronous and synchronous forms (both have asynchronous and synchronous formats).

Fs.readfile (file[, Options], callback)
    • file<String> filename
    • options <Object> | <String>
      • encoding<String> | <Null> default =null
      • flag<String> default =‘r‘
    • callback<Function>

If no encoding is specified and then the raw buffer is returned.

HTTP

To use the HTTP serverand client (server side and Clients ), one must require(‘http‘) .

The HTTP interfaces in node. JS is designed to support many features of the protocol which has been traditionally > difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses--the user was able to stream data.

Http.request (options[, callback])

optionsCan is an object or a string (object or String). If options is a string, it's automatically parsed with url.parse() .

The optional callback parameter is added as a one time listener for the ‘response‘ event.

Http.get (options[, callback])

Since Most requests is GET requests without bodies, node. JS provides this convenience method. the only difference (the difference from the request) between this method, and is, http.request() it sets the method to GET and calls req.end() automatically.

EVENT

In layman's node. JS (iv): node. js event mechanism

Much of the node. JS Core API is built around a idiomatic asynchronous Event-driven architecture in which certain kinds of Objects (called "emitters") periodically emit named events that cause Function objects ("listeners") to be called.

All objects this emit events EventEmitter is instances of the class ( an instance of Eventemitter ). These objects expose an eventEmitter.on() function ( these objects (emitter) are exposed. The On () method, which allows adding some listeners (function)) thatallows one or More Functions to is attached to named events emitted by the object.

eventEmitter.on()The method is used to register listeners (registered listener), while the eventEmitter.emit() method is used to trigger the event (excitation Event).

The event module (Events.eventemitter) is an implementation of a simple incident listener pattern. The method realizes the basic event listening mode such as Addlistener/on,once,removelistener,removealllisteners,emit.

var EventEmitter = require(‘events‘);

Emitter.addlistener (EventName, listener)

Alias for emitter.on(eventName, listener) .

Emitter.emit (eventname[, arg1][, arg2][, ...])

Synchronously calls each of the listeners registered for the event named eventName , in the order they were registered, passing The supplied arguments to each.

Returns if true the event had listeners (if there is a listener), false otherwise.

Emitter.listenercount (EventName)
    • eventName<Value> The name of the event being listened for

Returns the number of listeners listening to the event named eventName .

Emitter.listeners (EventName)

Returns a copy of the array of listeners for the event named eventName .

node. js API

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.