Vincec's Dimension

NodeJS Notes

Word count: 1,268 / Reading time: 8 min
2019/01/29 Share

Nodejs is asynchronously. It works in global namespace.

Console output

1
2
3
4
5
6
7
var hello = "Hello World";

console.log('Test ${hello}'); //Test ${hello}
console.log(`Test ${hello}`); //Test Hello World

console.log(__dirname);
console.log(__filename); //full path with filename

Import other module

path Module

1
2
3
4
var path = require("path");
console.log(`${path.basename(__filename)}`); //only file name

path.join(__dirname, 'dir1', 'dir2', 'dir3') // .../dir1/dir2/dir3

uitl Module

1
2
var util = require("util");
util.log(xxxxxx); // have timestamp

v8 Module

check memory usage

1
2
3
4
var v8 = require("v8");
var util = require("util");

util.log(v8.getHeapStatistics());

readline Module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);

rl.question("Question?", function(answer){
console.log(answer);
rl.setPrompt(`Question again after ${answer}?`);
rl.prompt(); //question again
rl.on('line', function(answer_2){ //new answer
console.log(answer_2);
rl.close(); //close readline
})
})

rl.on('close', function(){
//when rl is close, do this
process.exit(); //finally exit the process
})

event Module

ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var EventEmitter = require('events').EventEmitter;
var util = require('util'); // for inherits

var Person = function(name) {
this.name = name;
};

util.inherits(Person, EventEmitter);

var ben = new Person("Ben Franklin");

ben.on('speak', function(said) { //EventEmitter.on(), 注册监听器

console.log(`${this.name}: ${said}`);

});

ben.emit('speak', "You may delay, but time will not."); // EventEmitter.emit(), 调用监听器

child_process 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
var exec = require("child_process").exec; //for small process
exec("neofetch", function(err, stdout){
if(err){
throw err;
}

console.log("Neo Fetch");
console.log(stdout);
});

//-------

var spawn = require("child_process").spawn; //spawn for long and large process
var cp = spawn("node", ["alwaysTalking"]);

cp.stdout.on("data", function(data) {
console.log(`STDOUT: ${data.toString()}`);
});

cp.on("close", function() {
console.log("Child Process has ended");
process.exit();
});

setTimeout(function() {
cp.stdin.write("stop");
}, 4000);

fs Module

Read Dir

1
2
3
4
5
6
7
8
9
10
11
var fs = require("fs");
var files = fs.readdirSync('./lib');//read file synchronously
console.log(files);

//-------

fs.readdir('./lib', function(err, files){ //asynchronously, callback function envolve;
if(err){throw err;}
console.log(files);
});
console.log(files);

Read Files

1
2
3
4
5
6
7
8
9
10
11
12
13
var fs = require("fs");
var contents = fs.readFileSync("./lib/sayings.md", "UTF-8");//if no UTF-8, it will show binary
console.log(contents);

//async way
fs.readFile("./lib/sayings.md", "UTF-8", function(err, contents){
if(err){throw err;}
console.log(contents);
});


//---
var stats = fs.statSync(file); //stats.isFile() will show if it is file or dir

Readable file streams

To handle large data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var fs = require("fs");
var stream = fs.createReadStream("./chat.log", "UTF-8");
var data = "";

stream.on("data", function(chunk){
process.stdout.write(` chunk: ${chunk.length} |`);
data += chunk;
});
console.log(contents);

stream.once("data", function(){
console.log("\nStarting Reading File\n");
});

stream.on("end", function(){
console.log(`\nStarting Reading File ${data.length}\n`);
});

Create Files and append (writeFile)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var fs = require("fs");
var md = `

Sample Markdown Title
=====================

Sample subtitle
----------------

* point
* point
* point

`;

fs.writeFile("sample.md", md.trim(), function(err) {
console.log("File Created");
});

fs.appendFileSync("sample.md", `\nthis is a append contents`);

Write file in stream way

1
2
3
4
5
6
var fs = require("fs");
var stream = fs.createWriteStream(name + ".md");
stream.write("Start writing")
stream.write("| stream added")
//keep add stream
stream.close();

Create new Dir

1
2
3
4
5
6
7
8
9
10
11
12
var fs = require("fs");
if (fs.existsSync("lib")) { //check dir if is exist
console.log("Directory already there");
} else {
fs.mkdir("lib", function(err) { //can not work when dir exist
if (err) {
console.log(err);
} else {
console.log("Directory Created");
}
});
}

Rename & remove

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
var fs = require("fs");

//rename in Sync
fs.renameSync("./lib/project-config.js", "./lib/config.json");//rename
console.log("Config json file renamed");

//rename in Async, also work as remove
fs.rename("./lib/notes.md", "./notes.md", function(err) { //mv
if (err) {
console.log(err);
} else {
console.log("Notes.md moved successfully");
}
});

//remove in Sync way
try {
fs.unlinkSync("./lib/config.json");
} catch (err) {
console.log(err);
}

//remove in Async way
fs.unlink("notes.md", function(err) {
if (err) {
console.log(err);
} else {
console.log("Notes.md removed");
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var fs = require("fs");

//rename the dir, same as file

//remove the dir
fs.readdirSync("./logs").forEach(function(fileName) { //unlink/remove all the files before
fs.unlinkSync("./logs/" + fileName);
});

fs.rmdir("./logs", function(err) { //can not remove until it is empty
if (err) {
throw err;
}
console.log("Logs directory removed");
});

HTTP / HTTPS Module

HTTPS need to supply security certificate in server side

Create new module

save as /lib/Person.js

1
2
3
4
5
6
7
8
9
10
var EventEmitter = require('events').EventEmitter;
var util = require('util');

var Person = function(name) {
this.name = name;
};

util.inherits(Person, EventEmitter);

module.exports = Person;

load module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var Person = require("./lib/Person");

var ben = new Person("Ben Franklin");
var george = new Person("George Washington");

george.on('speak', function(said) {
console.log(`${this.name} -> ${said}`);
});

ben.on('speak', function(said) {
console.log(`${this.name}: ${said}`);
});

ben.emit('speak', "You may delay, but time will not.");
george.emit('speak', "It is far better to be alone, than to be in bad company.");

Process Objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//check current process arg varible
console.log(process.argv);

//simple grab function
function grab(flag) {
var index = process.argv.indexOf(flag);
return (index === -1) ? null : process.argv[index+1];
}

var greeting = grab('--greeting');
var user = grab('--user');

if (!user || !greeting) {
console.log("You Blew it!");
} else {
console.log(`Welcome ${user}, ${greeting}`);
}

//usage: node app --user Vince --greeting 'Hello World'

Console write

1
process.stdout.write("hello, world\n\n\n\n");

Console listener

1
2
3
4
//data listener
process.stdin.on('data', function(data){
process.stdout.write('\n', data.toString().trim() + '\n'); //repeat echo the input
});

Process Exit

process.exit()

1
2
3
4
//exit listener
process.on('exit', function(){
process.stdout.write('Process Exit'); //repeat echo the input
});

Timer

1
2
3
4
5
6
7
var waitTime = 3000;
console.log("wait"...);

//setTimeout once
setTimeout(function(){
console.log("done");
}, waitTime);
1
2
3
4
5
6
7
8
9
10
11
12
13
var currentTime = 0;
var waitInterval = 500;

//always call when interval reach
var interval = setInterval(function() {
currentTime += waitInterval;
console.log(`waiting ${currentTime/1000} seconds`);
}, waitInterval);

//to stop the interval after 3000
setTimeout(function(){
clearInterval(interval);
}, 3000);
1
2
3
4
//update current line inside each interval
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`waiting ... ${p}%`);

Author: VINCEC

Permalink: https://vince-amazing/blog/2019/01/29/nodejs-notes/

Date: January 29th 2019, 7:34:55

Copyright license: The article usingCC licensing 4.0

CATALOG
  1. 1. Console output
  2. 2. Import other module
    1. 2.1. path Module
    2. 2.2. uitl Module
    3. 2.3. v8 Module
    4. 2.4. readline Module
    5. 2.5. event Module
    6. 2.6. child_process Module
    7. 2.7. fs Module
      1. 2.7.1. Read Dir
      2. 2.7.2. Read Files
      3. 2.7.3. Readable file streams
      4. 2.7.4. Create Files and append (writeFile)
      5. 2.7.5. Write file in stream way
      6. 2.7.6. Create new Dir
      7. 2.7.7. Rename & remove
    8. 2.8. HTTP / HTTPS Module
  3. 3. Create new module
  4. 4. Process Objects
    1. 4.1. Console write
    2. 4.2. Console listener
    3. 4.3. Process Exit
  5. 5. Timer