NodeJS by examples
Understanding require
[Link]
var circle = require('./[Link]');
[Link]( 'The area of a circle of radius 4 is
'
+ [Link](4));
[Link] (in the same folder)
var PI = [Link];
[Link] = function (r) {
return PI * r * r;
};
[Link] = function (r) {
return 2 * PI * r;
};
Node core modules
The core modules are defined in
node's source in the lib/ folder.
They are always given a preference
in loading. This means that you
should never name you file with the
same name as that of the inbuild
module.
Pecking order of loading
modules*
If the file at '/home/ry/projects/[Link]'
called require('[Link]'), then node would
look in the following locations, in this
order:
/home/ry/projects/node_modules/[Link]
/home/ry/node_modules/[Link]
/home/node_modules/[Link]
/node_modules/[Link]
*Source: [Link]
Pre-requisite
Install NodeJS from
[Link]
Node v to check if node is already
installed.
Pick you favorite editor (I am
experimenting with Microsoft Visual
Code)
Examples from
Learnyounode:
[Link]
nyounode
NodeJS by example
Problem 1: [Link]
Solution
[Link]("HELLO WORLD");
Sync call on console
Write a program that uses a single
synchronous filesystem operation to
read a file and print the number of
newlines (\n) it contains to the
console (stdout)
Hint
require('fs')
[Link][2]
readFileSync
Async call on console
Write a program that uses a single
asynchronous filesystem operation to
read a file and print the number of
newlines it contains to the console
(stdout)
Hint
require('fs')
readdir