0%

Nodejs API 笔记

Local System

File System

File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require('fs'). All the methods have asynchronous and synchronous forms.

1
2
3
4
5
6
7
8
9
10
11
const fs = require('fs');

// Asynchronous
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});

// Synchronous
fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
Path

This module contains utilities for handling and transforming file paths. Use require(‘path’) to use this module.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var path = require('path');

// returns 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html');

// The platform-specific path delimiter, ; or ':'
console.log(path.delimiter);

// returns '/foo/bar/baz/asdf'
path.dirname('/foo/bar/baz/asdf/quux')

// returns '.html'
path.extname('index.html')
// returns '.md'
path.extname('index.coffee.md')
// returns '.'
path.extname('index.')
// returns ''
path.extname('index')
// returns ''
path.extname('.index')

// returns '/home/user/dir/file.txt'
path.format({
root : "/",
dir : "/home/user/dir",
base : "file.txt",
ext : ".txt",
name : "file"
});

// returns '/foo/bar/baz/asdf'
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')

// returns '/foo/bar/baz/asdf'
path.normalize('/foo/bar//baz/asdf/quux/..')

// returns
// {
// root : "/",
// dir : "/home/user/dir",
// base : "file.txt",
// ext : ".txt",
// name : "file"
// }
path.parse('/home/user/dir/file.txt')
Child Process

The child_process module provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
OS

Provides a few basic operating-system related utility functions.

Network

HTTP

The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. To use the HTTP server and client one must require('http').

URL

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

Example: ‘http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

  • href: ‘http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
  • protocol: ‘http:’
  • slashes: true or false (The protocol requires slashes after the colon)
  • host: ‘host.com:8080’
  • auth: ‘user:pass’
  • hostname: ‘host.com’
  • port: ‘8080’
  • pathname: ‘/p/a/t/h’
  • search: ‘?query=string’
  • path: ‘/p/a/t/h?query=string’
  • query: ‘query=string’ or {‘query’:’string’}
  • hash: ‘#hash’
1
2
3
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'

Global Objects

These objects are available in all modules.

Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

1
2
3
4
5
6
7
8
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
Exports & Modules

TODO: …

Process

The process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter.

Timers

TODO: …

Misc.

ArrayBuffer

The ArrayBuffer is a data type that is used to represent a generic, fixed-length binary data buffer.

1
2
3
4
5
var buffer = new ArrayBuffer(16);
var int32View = new Int32Array(buffer);
for (var i = 0; i < int32View.length; i++) {
int32View[i] = i * 2;
}
Events

Much of the Node.js core API is built around an 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.

1
2
3
4
5
6
7
8
9
const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');