Skip to content

Commit 5040905

Browse files
committed
Node API
1 parent 459647b commit 5040905

File tree

1 file changed

+38
-55
lines changed

1 file changed

+38
-55
lines changed

nodejs-api.md

+38-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
## Node.js APIs
2-
3-
4-
5-
#### Global Objects
1+
# Node APIs List
2+
3+
- [Global Objects](#global-objects)
4+
- [Console](#console)
5+
- [Timers](#timers)
6+
- [Modules](#modules)
7+
- [Process](#process)
8+
- [Child Process](#child-process)
9+
- [Util](#util)
10+
- [Events](#events)
11+
- [Stream](#stream)
12+
- [File System](#file-system)
13+
- [Path](#path)
14+
- [HTTP](#http)
15+
- [URL](#url)
16+
- [Query String](#query-string)
17+
- [Assert](#assert)
18+
- [OS](#os)
19+
- [Buffer](#buffer)
20+
21+
## Global Objects
622

723
In browsers, the top-level scope is the global scope.
824
That means that in browsers if you're in the global scope var something will define a global variable.
@@ -17,8 +33,7 @@ In Node this is different. The top-level scope is not the global scope; var some
1733
|process; | The process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter.|
1834
|Buffer; | The Buffer class is a global type for dealing with binary data directly.|
1935

20-
21-
#### Console
36+
## Console
2237

2338
| API | Description |
2439
|---------------------------------------|---------------------------------|
@@ -32,9 +47,7 @@ In Node this is different. The top-level scope is not the global scope; var some
3247
|console.trace(label); |Print a stack trace to stderr of the current position.|
3348
|console.assert(expression, [message]); |Same as assert.ok() where if the expression evaluates as false throw an AssertionError with message.|
3449

35-
36-
#### Timers
37-
50+
## Timers
3851

3952
| API | Description |
4053
|---------------------------------------------|-------------------------------------|
@@ -47,9 +60,7 @@ In Node this is different. The top-level scope is not the global scope; var some
4760
|unref(); | Allow you to create a timer that is active but if it is the only item left in the event loop, node won't keep the program running.|
4861
|ref(); | If you had previously unref()d a timer you can call ref() to explicitly request the timer hold the program open.
4962

50-
51-
#### Modules
52-
63+
## Modules
5364

5465
```javascript
5566
var module = require('./module.js'); // Loads the module module.js in the same directory.
@@ -81,8 +92,7 @@ module.exports = function(width) {
8192
}
8293
```
8394

84-
#### Process
85-
95+
## Process
8696

8797
```javascript
8898
process.on('exit', function(code) {}); // Emitted when the process is about to exit
@@ -124,13 +134,11 @@ process.on('uncaughtException', function(err) {}); // Emitted when an exception
124134
|process.uptime(); |Number of seconds Node has been running.|
125135
|process.hrtime(); |Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array.|
126136

137+
## Child Process
127138

128-
#### Child Process
129-
130139
Node provides a tri-directional popen facility through the child_process module.
131140
It is possible to stream data through a child's stdin, stdout, and stderr in a fully non-blocking way.
132141

133-
134142
| API | Description |
135143
|--------------------------|--------------------------------------|
136144
ChildProcess; |Class. ChildProcess is an EventEmitter|
@@ -147,12 +155,10 @@ child_process.exec(command, [options], callback); |Runs a command in
147155
child_process.execFile(file, [args], [options], [callback]); |Runs a command in a shell and buffers the output.|
148156
child_process.fork(modulePath, [args], [options]); |This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. |
149157

150-
151-
#### Util
158+
## Util
152159

153160
These functions are in the module 'util'. Use require('util') to access them.
154161

155-
156162
| API | Description |
157163
|-----------------------------|--------------------------------------|
158164
|util.format(format, [...]); |Returns a formatted string using the first argument as a printf-like format. (%s, %d, %j)|
@@ -168,14 +174,12 @@ These functions are in the module 'util'. Use require('util') to access them.
168174
|util.isError(object); |Returns true if the given "object" is an Error. false otherwise.|
169175
|util.inherits(constructor, superConstructor); |Inherit the prototype methods from one constructor into another.|
170176

171-
172-
#### Events
177+
## Events
173178

174179
All objects which emit events are instances of events.EventEmitter. You can access this module by doing: require("events");
175180
To access the EventEmitter class, require('events').EventEmitter.
176181
All EventEmitters emit the event 'newListener' when new listeners are added and 'removeListener' when a listener is removed.
177182

178-
179183
| API | Description |
180184
|---------------------------------------|--------------------------------------|
181185
|emitter.addListener(event, listener); |Adds a listener to the end of the listeners array for the specified event.|
@@ -188,12 +192,10 @@ All EventEmitters emit the event 'newListener' when new listeners are added and
188192
|emitter.emit(event, [arg1], [arg2], [...]); |Execute each of the listeners in order with the supplied arguments. Returns true if event had listeners, false otherwise.|
189193
|EventEmitter.listenerCount(emitter, event); |Return the number of listeners for a given event.|
190194

191-
192-
#### Stream
195+
## Stream
193196

194197
A stream is an abstract interface implemented by various objects in Node. For example a request to an HTTP server is a stream, as is stdout. Streams are readable, writable, or both. All streams are instances of EventEmitter.
195198

196-
197199
The Readable stream interface is the abstraction for a source of data that you are reading from.
198200
In other words, data comes out of a Readable stream.
199201
A Readable stream will not start emitting data until you indicate that you are ready to receive it.
@@ -203,6 +205,7 @@ zlib streams, crypto streams, tcp sockets, child process stdout and stderr, proc
203205
```javascript
204206
var readable = getReadableStreamSomehow();
205207
```
208+
206209
| API | Description |
207210
|---------------------------------------|--------------------------------------|
208211
|readable.on('readable', function() {}); |When a chunk of data can be read from the stream, it will emit a 'readable' event|
@@ -217,7 +220,6 @@ var readable = getReadableStreamSomehow();
217220
|readable.unpipe([destination]); |This method will remove the hooks set up for a previous pipe() call. If the destination is not specified, then all pipes are removed.|
218221
|readable.unshift(chunk); |This is useful in certain cases where a stream is being consumed by a parser, which needs to "un-consume" some data that it has optimistically pulled out of the source, so that the stream can be passed on to some other party.|
219222

220-
221223
The Writable stream interface is an abstraction for a destination that you are writing data to.
222224
Examples of writable streams include: http requests on the client, http responses on the server, fs write streams,
223225
zlib streams, crypto streams, tcp sockets, child process stdin, process.stdout, process.stderr.
@@ -244,13 +246,11 @@ Transform streams are Duplex streams where the output is in some way computed fr
244246

245247
Examples of Transform streams include: zlib streams, crypto streams.
246248

247-
248-
#### File System
249+
## File System
249250

250251
To use this module do require('fs').
251252
All the methods have asynchronous and synchronous forms.
252253

253-
254254
| API | Description |
255255
|------------------------------------------------|--------------------------------------|
256256
|fs.rename(oldPath, newPath, callback); |Asynchronous rename. No arguments other than a possible exception are given to |the completion callback.Asynchronous ftruncate. No arguments other than a possible exception are given to the completion callback.|
@@ -325,21 +325,18 @@ All the methods have asynchronous and synchronous forms.
325325
|stats.isFIFO()| |
326326
|stats.isSocket()| |
327327

328-
329328
```javascript
330329
fs.createReadStream(path, [options]); // Returns a new ReadStream object.
331330
fs.createWriteStream(path, [options]); // Returns a new WriteStream object.
332331
```
333332

334-
335-
#### Path
333+
## Path
336334

337335
Use require('path') to use this module.
338336
This module contains utilities for handling and transforming file paths.
339337
Almost all these methods perform only string transformations.
340338
The file system is not consulted to check whether paths are valid.
341339

342-
343340
| API | Description |
344341
|------------------------------------------------|--------------------------------------|
345342
|path.normalize(p); |Normalize a string path, taking care of '..' and '.' parts.|
@@ -352,12 +349,10 @@ The file system is not consulted to check whether paths are valid.
352349
|path.sep; |The platform-specific file separator. '\\' or '/'.|
353350
|path.delimiter; |The platform-specific path delimiter, ';' or ':'.|
354351

355-
356-
#### HTTP
352+
## HTTP
357353

358354
To use the HTTP server and client one must require('http').
359355

360-
361356
| API | Description |
362357
|------------------------------------------------|--------------------------------------|
363358
|http.STATUS_CODES; |A collection of all the standard HTTP response status codes, and the short description of each.|
@@ -412,36 +407,29 @@ To use the HTTP server and client one must require('http').
412407
|message.socket; |The net.Socket object associated with the connection.|
413408
|message.setTimeout(msecs, callback); |Calls message.connection.setTimeout(msecs, callback).|
414409

415-
416-
417-
#### URL
410+
## URL
418411

419412
This module has utilities for URL resolution and parsing. Call require('url') to use it.
420413

421-
422414
| API | Description |
423415
|------------------------------------------------|--------------------------------------|
424416
|url.parse(urlStr, [parseQueryString], [slashesDenoteHost]); |Take a URL string, and return an object.|
425417
|url.format(urlObj); |Take a parsed URL object, and return a formatted URL string.|
426418
|url.resolve(from, to); |Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.|
427419

428-
429-
#### Query String
420+
## Query String
430421

431422
This module provides utilities for dealing with query strings. Call require('querystring') to use it.
432423

433-
434424
| API | Description |
435425
|------------------------------------------------|--------------------------------------|
436426
|querystring.stringify(obj, [sep], [eq]); | Serialize an object to a query string. Optionally override the default separator ('&') and assignment ('=') characters.|
437427
|querystring.parse(str, [sep], [eq], [options]); | Deserialize a query string to an object. Optionally override the default |separator ('&') and assignment ('=') characters.|
438428

439-
440-
#### Assert
429+
## Assert
441430

442431
This module is used for writing unit tests for your applications, you can access it with require('assert').
443432

444-
445433
| API | Description |
446434
|----------------------------------------------------|--------------------------------------|
447435
|assert.fail(actual, expected, message, operator); |Throws an exception that displays the values for actual and expected separated by the provided operator.|
@@ -456,13 +444,11 @@ This module is used for writing unit tests for your applications, you can access
456444
|assert.doesNotThrow(block, [message]); |Expects block not to throw an error, see assert.throws for details.|
457445
|assert.ifError(value); |Tests if value is not a false value, throws if it is a true value. |Useful when testing the first argument, error in callbacks.|
458446

459-
460-
#### OS
447+
## OS
461448

462449
Provides a few basic operating-system related utility functions.
463450
Use require('os') to access this module.
464451

465-
466452
| API | Description |
467453
|-------------------------|--------------------------------------|
468454
|os.tmpdir(); |Returns the operating system's default directory for temp files.
@@ -480,12 +466,10 @@ Use require('os') to access this module.
480466
|os.networkInterfaces(); |Get a list of network interfaces.
481467
|os.EOL; |A constant defining the appropriate End-of-line marker for the operating system.
482468

483-
484-
#### Buffer
469+
## Buffer
485470

486471
Buffer is used to dealing with binary data. Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap
487472

488-
489473
| API | Description |
490474
|-------------------------|--------------------------------------|
491475
|new Buffer(size); |Allocates a new buffer of size octets.|
@@ -504,4 +488,3 @@ Buffer is used to dealing with binary data. Buffer is similar to an array of int
504488
|buf[index]; |Get and set the octet at index|
505489
|buf.length; |The size of the buffer in bytes, Note that this is not necessarily the size of the contents|
506490
|buffer.INSPECT_MAX_BYTES; |How many bytes will be returned when buffer.inspect() is called. This can be overridden by user modules.|
507-

0 commit comments

Comments
 (0)