SlideShare a Scribd company logo
NodeJS by examples
Understanding require
• Foo.js
• circle.js (in the same folder)
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
+ circle.area(4));
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/learnyouno
de
NodeJS by example
• Problem 1: HelloWorld.js
• console.log("HELLO WORLD");
Solution
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

More Related Content

What's hot

Drupal Coding Standards - do and don't
Drupal Coding Standards - do and don'tDrupal Coding Standards - do and don't
Drupal Coding Standards - do and don't
Arunkumar Kupppuswamy
 
Android APP-toggle case
Android APP-toggle caseAndroid APP-toggle case
Android APP-toggle case
Liang (Leon) Zhou
 
Glusterfs session #13 replication introduction
Glusterfs session #13   replication introductionGlusterfs session #13   replication introduction
Glusterfs session #13 replication introduction
Pranith Karampuri
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing filesroot_fibo
 
Unit3 browsing the filesystem
Unit3 browsing the filesystemUnit3 browsing the filesystem
Unit3 browsing the filesystemroot_fibo
 
Memory Analyzer Tool (MAT)
Memory Analyzer Tool (MAT) Memory Analyzer Tool (MAT)
Memory Analyzer Tool (MAT)
Samiullah Farooqui
 
STOR2RRD presentation from Common CZ/SK 2015
STOR2RRD presentation from Common CZ/SK 2015STOR2RRD presentation from Common CZ/SK 2015
STOR2RRD presentation from Common CZ/SK 2015
Pavel Hampl
 
Beyondfs-intro
Beyondfs-introBeyondfs-intro
Beyondfs-intro
Kim Yong-Duk
 
Drush
DrushDrush
Vfs
VfsVfs

What's hot (11)

Drupal Coding Standards - do and don't
Drupal Coding Standards - do and don'tDrupal Coding Standards - do and don't
Drupal Coding Standards - do and don't
 
Android APP-toggle case
Android APP-toggle caseAndroid APP-toggle case
Android APP-toggle case
 
Glusterfs session #13 replication introduction
Glusterfs session #13   replication introductionGlusterfs session #13   replication introduction
Glusterfs session #13 replication introduction
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing files
 
Unit3 browsing the filesystem
Unit3 browsing the filesystemUnit3 browsing the filesystem
Unit3 browsing the filesystem
 
Memory Analyzer Tool (MAT)
Memory Analyzer Tool (MAT) Memory Analyzer Tool (MAT)
Memory Analyzer Tool (MAT)
 
STOR2RRD presentation from Common CZ/SK 2015
STOR2RRD presentation from Common CZ/SK 2015STOR2RRD presentation from Common CZ/SK 2015
STOR2RRD presentation from Common CZ/SK 2015
 
Beyondfs-intro
Beyondfs-introBeyondfs-intro
Beyondfs-intro
 
MongoDB
MongoDBMongoDB
MongoDB
 
Drush
DrushDrush
Drush
 
Vfs
VfsVfs
Vfs
 

Similar to Node js Lunch and learn: Session 2

Full metal mongo
Full metal mongoFull metal mongo
Full metal mongo
Israel Gutiérrez
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHPPaul Jones
 
Android OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final ReportAndroid OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final Report
Jungsoo Nam
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and Execution
Chong-Kuan Chen
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain
艾鍗科技
 
AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondZubair Nabi
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
cacois
 
D7 entities fields
D7 entities fieldsD7 entities fields
D7 entities fields
cyberswat
 
第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディングnobu_k
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
Lifna C.S
 
iOS: Using persistant storage
iOS: Using persistant storageiOS: Using persistant storage
iOS: Using persistant storageJussi Pohjolainen
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
YourHelper1
 
00-intro-to-classes.pdf
00-intro-to-classes.pdf00-intro-to-classes.pdf
00-intro-to-classes.pdf
TamiratDejene1
 
Erlocator
ErlocatorErlocator
Summary of JDK10 and What will come into JDK11
Summary of JDK10 and What will come into JDK11Summary of JDK10 and What will come into JDK11
Summary of JDK10 and What will come into JDK11
なおき きしだ
 
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
Kai Chan
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
The Dojo Toolkit An Introduction
The Dojo Toolkit   An IntroductionThe Dojo Toolkit   An Introduction
The Dojo Toolkit An Introduction
Jeff Fox
 

Similar to Node js Lunch and learn: Session 2 (20)

Full metal mongo
Full metal mongoFull metal mongo
Full metal mongo
 
Fedora4
Fedora4Fedora4
Fedora4
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
 
Android OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final ReportAndroid OpenGL ES Game ImageGrabber Final Report
Android OpenGL ES Game ImageGrabber Final Report
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and Execution
 
嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain嵌入式Linux課程-GNU Toolchain
嵌入式Linux課程-GNU Toolchain
 
AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyond
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
D7 entities fields
D7 entities fieldsD7 entities fields
D7 entities fields
 
第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング第一回MongoDBソースコードリーディング
第一回MongoDBソースコードリーディング
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
iOS: Using persistant storage
iOS: Using persistant storageiOS: Using persistant storage
iOS: Using persistant storage
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
25dom
25dom25dom
25dom
 
00-intro-to-classes.pdf
00-intro-to-classes.pdf00-intro-to-classes.pdf
00-intro-to-classes.pdf
 
Erlocator
ErlocatorErlocator
Erlocator
 
Summary of JDK10 and What will come into JDK11
Summary of JDK10 and What will come into JDK11Summary of JDK10 and What will come into JDK11
Summary of JDK10 and What will come into JDK11
 
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
Search Engine Building with Lucene and Solr (So Code Camp San Diego 2014)
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
The Dojo Toolkit An Introduction
The Dojo Toolkit   An IntroductionThe Dojo Toolkit   An Introduction
The Dojo Toolkit An Introduction
 

Node js Lunch and learn: Session 2