OSDN Git Service

Regular updates
[twpd/master.git] / nodejs.md
1 ---
2 title: Node.js API
3 category: Node.js
4 layout: 2017/sheet
5 ---
6
7 ### Globals
8
9     __filename
10     __dirname
11
12 ### exec
13
14     var exec = require('child_process').exec,
15
16     var child = exec('cat *.js bad_file | wc -l',
17       function (error, stdout, stderr) {
18         console.log('stdout: ' + stdout);
19         console.log('stderr: ' + stderr);
20         if (error !== null) {
21           console.log('exec error: ' + error);
22         }
23     });
24
25 ### Snippets
26
27     info = require('../package.json')
28     info.version
29
30     process.stdout.write(util.inspect(objekt, false, Infinity, true) + '\n');
31
32 ### Spawn - passthru the in/out
33
34     var spawn = require('child_process').spawn;
35     var proc = spawn(bin, argv, { stdio: 'inherit' });
36     proc.on('error', function(err) {
37       if (err.code == "ENOENT") { "does not exist" }
38       if (err.code == "EACCES") { "not executable" }
39     });
40     proc.on('exit', function(code) { ... });
41
42     // also { stdio: ['pipe', 'pipe', process.stdout] }
43     // also { stdio: [process.stdin, process.stderr, process.stdout] }
44
45     proc.stdout.on('data', function (data) {
46     });
47     proc.stderr.on('data', function (data) {
48     });
49
50 [all]: http://nodejs.org/api/all.html