DEV Community

Ruxin Qu
Ruxin Qu

Posted on

JavaScript: Node.js (built in modules)

  1. An error must be thrown to halt the program. console.log(new Error('this is an error')) doesn't stop program from running.
  2. console.table() print out the elements in an object as a table

  3. console.assert() print out false if the statement in the parenthesis is falsy.

  4. process is a global module. (Buffer, Error are also global module)

process.argv[0] returns the path of node;
process.argv[1] returns the file name;
process.argv[2] returns the first word in user input;
Enter fullscreen mode Exit fullscreen mode

5.node uses error-first call back function

const callbackFunc = (err, data)=>{err? console.log(err): console.log(data)}
Enter fullscreen mode Exit fullscreen mode

6.fs is the built in module to interact with file system
Difference between fs.writeFile() and fs.createWriteStream()

  • fs.writeFile() requires all the content of the file at once;
  • fs.createWriteStream() supports sequential writing.
const writeableStream = fs.createWriteStream('text.txt')
process.stdin.pipe(writableStream);
//output: all the input from terminal when the terminal is open.
Enter fullscreen mode Exit fullscreen mode
  • fs.createWriteStream() can also work with fs.createReadStream() to copy file from one to another
let readableStream = fs.createReadStream("test.txt");
let writableStream = fs.createWriteStream("test2.txt");

readableStream.on("data", function(chunk) {
    writableStream.write(chunk);
});
Enter fullscreen mode Exit fullscreen mode

7.event module provides an EventEmitter class. Example of creating an instance of EventEmitter class:

const events = require('events');
const myEmitter = new events.EventEmitter();
Enter fullscreen mode Exit fullscreen mode
  • Each instance has a .on() method which assigns a callback function to a named event. The first argument is the name of the event as a string, the second argument is the callback.
  • Each instance also has a .emit() method which announces the named event has occured. The first argument is the name of the event, the second argument is the data that should be passed into the callback function.

8.console.log() is a thin wrapper of process.stdout.write(), to receive input from a terminal, use process.stdin.on()
9.Buffer module has several methods to handle binary data.

  • .alloc() creates a new Buffer object.
  • .toString() transfer the Buffer object to a string.
  • .from() can create an Buffer from a string
  • .concat() joins all the buffer objects in an array into one buffer object.(this method comes in handy because a Buffer object can't be resized.)
const buffer = Buffer.alloc(5,'b');
console.log(buffer); //output: <Buffer 62 62 62 62 62>
console.log(buffer.toString()) //output: bbbbb
const buffer1 = Buffer.from('hello')
const buffer2 = Buffer.from('world')
console.log(buffer1) //output: <Buffer 68 65 6c 6c 6f>
const bufferConcat = Buffer.concat([buffer1, buffer2])
console.log(bufferConcat) //output: <Buffer 68 65 6c 6c 6f 77 6f 72 6c 64>
Enter fullscreen mode Exit fullscreen mode

10.setImmediate() execute the callback function inside after the current poll is completed.

Top comments (0)