Node.js Tutorial
This page is a tutorial of node.js.
You should already know the basics of JavaScript. If not, see
Install
Go to http://nodejs.org/ site, and download the binary. There's a binary for Linux too, no need to compile.
Run node
Once installed, go to terminal, type node
to start the command line.
To run a js script, type:
node filename
globals
There are these global objects:
global
process
console
module
exports
__filename
- fullpath of current script
__dirname
- dir path of current script
For full list, see: Global Objects
// print fullpath of current script console.log(__filename); // print dir path of current script console.log(__dirname);
global.bb = 4; console.log(global.bb); // 4
Use console.dir(…)
to inspect objects, including builtin Node objects. This is handy to see what property/methods a object has.
console.dir(obj)
console.dir(global); // huge output. try it.
console.dir(…)
is actually a wrapper to util.inspect()
method.
The “inspect” takes some useful arguments.
// print all properties of the require('net') module, using inspect method from module util console.log( require('util').inspect( require('net'), { showHidden: true, depth: null }) );
util.inspect(object, [options])
show node.js version from runtime
show node.js version from runtime
// show node.js version from runtime console.log( process.version ); // v12.10.0
command line args
Use process.argv
to get arguments from command line.
// sum numbers from command line args // save this file as x.js // run it in terminal, type // node x.js 3 4 // prints 7 const tt = process.argv; tt.splice(0,2); console.log( tt.reduce( ((x,y) => (parseInt(x) + parseInt(y))) ) );
count lines (sync version)
// open file, count number of newlines // save this file as x.js // run it in terminal, type // node x.js filepath // prints number of newline chars const fs = require('fs'); const fpath = process.argv[2]; const buf = fs.readFileSync(fpath); const ss = buf.toString("utf8"); console.log( ss.match(/\n/g).length );
count lines (async version)
// open file, count number of newlines. async. // save this file as x.js // run it in terminal, type // node x.js filepath // prints number of newline chars const fs = require('fs'); const fpath = process.argv[2]; fs.readFile(fpath, ((err, data) => { if (err) {throw err;} else { count = data.toString("utf8").match(/\n/g).length; console.log(count); }}));
fs.readFile(filename, [options], callback)
list files
// given a dir path and extension such as "txt", print file names with that extension const ff = require('fs'); const pp = require('path'); const inputDir = "/home/joe/"; const fExt = "txt"; const printFileName = ((err, fileList) => { if (err) {throw err;} else { fileList.filter( (x => (pp.extname(x) === "." + fExt)) ) .forEach( (x => (console.log(x))) ) } }); ff.readdir(inputDir, printFileName );
modules
Each module corresponds to a file.
Each module and its file have the same name.
Net Module for writing TCP Server
net.createServer()
return a server object.
net.connect(…)
and
net.createConnection(…)
are aliases.
net.createConnection(…)
return a socket object.
See also: Node.js Video Tutorial by Ryan Dahl