0% found this document useful (0 votes)
51 views12 pages

NodeJS 2

The document discusses NodeJS concepts like requiring modules, core modules, module loading order, and provides examples from learnyounode on building programs that perform synchronous and asynchronous filesystem operations to count newlines in files. It recommends installing NodeJS from its website and experimenting with the Microsoft Visual Code editor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views12 pages

NodeJS 2

The document discusses NodeJS concepts like requiring modules, core modules, module loading order, and provides examples from learnyounode on building programs that perform synchronous and asynchronous filesystem operations to count newlines in files. It recommends installing NodeJS from its website and experimenting with the Microsoft Visual Code editor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NodeJS by examples

Understanding require
Foo.js

var circle = require('./circle.js');


console.log( 'The area of a circle of radius 4 is
'
+ circle.area(4));

circle.js (in the same folder)


var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = 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/foo.js'
called require('bar.js'), then node would
look in the following locations, in this
order:

/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js

*Source: http://nodejs.org/docs/v0.4.2/api/modules.html

Pre-requisite
Install NodeJS from
https://nodejs.org/
Node v to check if node is already
installed.
Pick you favorite editor (I am
experimenting with Microsoft Visual
Code)

Examples from
Learnyounode:
https://github.com/workshopper/lear
nyounode

NodeJS by example
Problem 1: HelloWorld.js

Solution
console.log("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')
process.argv[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

You might also like