Node's Common Built-in Modules

Working with the Operating System

const os = require('os');

os.EOL

os.arch

os.constants

os.cpus

os.endianness

os.freemem

os.getNetworkInterfaces

os.homedir

os.hostname

os.loadavg

os.networkInterfaces

os.platform

os.release

os.tmpDir

os.tmpdir

os.totalmem

os.type

os.uptime
os.userInfo

 

Working with the File System

const fs = require('fs');

Asynchronous

vs

Synchronous

const fs = require('fs');

// Asynchronous Form:
fs.readFile(__filename, (err, data) => {
  if (err) throw err;

  // do something with data
});

Asynchronous

const fs = require('fs');

// Synchronous Form:
const data = fs.readFileSync(__filename);
// exceptions are immediately thrown
// do something with data

Synchronous

fs.F_OK

fs.FileReadStream

fs.FileWriteStream

fs.R_OK

fs.ReadStream

fs.Stats

fs.SyncWriteStream

fs.W_OK

fs.WriteStream
fs.X_OK

fs._toUnixTimestamp

fs.access
fs.accessSync

fs.appendFile

fs.appendFileSync
fs.chmod

fs.chmodSync

fs.chown
fs.chownSync

fs.close

fs.closeSync
fs.constants

fs.createReadStream   fs.createWriteStream
fs.exists

fs.existsSync

fs.fchmod
fs.fchmodSync

fs.fchown

fs.fchownSync
fs.fdatasync

fs.fdatasyncSync

fs.fstat

fs.fstatSync

fs.fsync

fs.fsyncSync
fs.ftruncate

fs.ftruncateSync

fs.futimes
fs.futimesSync

fs.link

fs.linkSync
fs.lstat

fs.lstatSync

fs.mkdir
fs.mkdirSync

fs.mkdtemp

fs.mkdtempSync
fs.open

fs.openSync

fs.read
fs.readFile

fs.readFileSync

fs.readSync
fs.readdir

fs.readdirSync

fs.readlink
fs.readlinkSync

fs.realpath

fs.realpathSync
fs.rename

fs.renameSync

fs.rmdir
fs.rmdirSync

fs.stat

fs.statSync

fs.symlink

fs.symlinkSync

fs.truncate
fs.truncateSync         fs.unlink               fs.unlinkSync
fs.unwatchFile           fs.utimes               fs.utimesSync
fs.watch                 fs.watchFile             fs.write

fs.writeFile             fs.writeFileSync         fs.writeSync

TASK 1:

Script to fix files in a directory. Each file has its data duplicated. Truncate each file in half.

TASK 2:

Script to clean old files in a directory. Anything older than 7 days should be deleted.

Console and Utilities

console.Console

Util

node

> console.log('One %s', 'thing');

 

>util.format('One %s', 'thing');

 

 

> console.log(module);

> util.inspect(module);

console.assert(3 == '3);

console.trace('here');

Debugging Node.js Applications

node debug index.js

node --inspect --debug-brk index.js

node-inspector

Node's Common Built-in Modules

By Shuhratbek Mamadaliyev

Node's Common Built-in Modules

  • 942