Download presentation
Presentation is loading. Please wait.
1
Week 05 Node.js
2
What is Node.js? Node.js provides a non-blocking architecture using event loop processing based on single threaded JavaScript model and built on top of Google’s V8 JavaScript Engine. Event loop processing in Node.js is a programming style where the flow of execution is determined by events. Events are handled by event handlers or event callbacks. An event callback is a function that is invoked when something significant happens such as when the result of a database query is available. Therefore, Node.js is an event-driven or asynchronous programming. This means the current process will not block when it is doing I/O. Therefore, several I/O operations can occur in parallel, and each respective callback function will be invoked when the operation finishes. It’s important to know that event loop in Node.js is just one thread running inside one process, which means that, when an event happens, the event handler can run without interruption. In other words, there is at most one event handler running at any given time and any event handler will run to completion without being interrupted. Node’s core functionalities are kept to a minimum. There are many third-party modules supporting Node.js and can be downloaded, installed, and managed using Node Package Manager (NPM) to expand node’s functionalities.
3
Node.js Background Node.js was invented in 2009 by Ryan Dahl along with other developers while working at Joyent. The Node.js’ Package Manager (NPM) was introduced in 2011, allowing publishing and sharing node.js source code by the community. Node.js was originally developed for the Linux environment. The first node.js to support Windows was released in July 2011. Node.js went through several technical changes including internal conflict over Joyent’s governance, resulting in Fedor Indutny (Node.js developer) starting io.js, a fork of Node.js. In February 2015, the Node.js foundation was announced, providing the opportunity of merging Node.js and io.js communities together under the Node.js foundation. On 10/12/2015, Node (LTS) was announced, the first release covered under the new LTS plan (Long Term Support).
4
Features of Node.js Asynchronous and Event Driven: All APIs of Node.js library are asynchronous and non-blocking. Node.js server never waits for an API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call. Single-Threaded and Highly Parallel: Other systems try to gain parallelism by running lots of code at the same time, typically by spawning many threads. But not Node.js, Node is a single-threaded environment. At most, only one line of your code will ever be executing at any time. Node gets away with this by doing most I/O tasks using non-blocking techniques. Rather than waiting line-by-line for an operation to finish, you create a callback that will be invoked when the operation eventually succeed or fails.
5
Overview of Blocking vs Non-Blocking
Blocking is when the executing of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. This happens because the event loop is unable to continue running JavaScript while a blocking operation is occurring. Synchronous methods in the Node.js are the most commonly used blocking operations. (Native modules may also have blocking methods) All of the I/O methods in the Node.js standard library provide asynchronous versions, which are non-blocking, and accept callback functions. Some methods also have blocking counterparts, which have names that end with sync.
6
Comparing Code Blocking methods execute synchronously and non-blocking methods executes asynchronously. Using the File System modules, this is a synchronous file read: const fs = require("fs"); const data = fs.readFileSync("/test.txt"); // blocking Here is an equivalent asynchronous example (non-blocking): fs.readFile("/test.txt", function (err, data) { if(err) throw err; });
7
Blocking vs Non-Blocking codes
Using the File System modules, this is a synchronous file read: const fs = require("fs"); const data = fs.readFileSync("/test.txt"); // blocks until file is read console.log(data); moreWork(); // will run after console.log Here is an equivalent asynchronous example: fs.readFile("/test.txt", (err, data) => { if(err) throw err; }); moreWork(); // will run before console.log
8
Download and Install Node.js
Download and install latest version of Node.js Instruction for windows O/S click here ( Instruction for Mac O/S click here ( Create a working folder (nodejs_workspace). Check the Node.js version that just installed. c:\nodejs_workspace>node --version (or node –v)
9
Verify Installation Create a js file named hello_world.js in c:\nodejs_workspace with the following statement: console.log("Hello world!"); Now run the hello_world.js: C:\nodejs_workspace>node hello_world.js Verify the Output: Hello World!
10
Node.js - REPL REPL stands for Read Eval Print Loop.
It acts like a window console or UNIX/Linux shell where a command is entered and system responds with an output. Read – Reads user’s input, parse the input into JavaScript data-structure and stores in memory. Eval – Evaluates the data structure. Print – Prints the result. Loop – Loops the above command until user press ctrl-c twice. NOTE: REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.
11
How to start the REPL in Node?
REPL can be started by simply running node on shell/console without any argument. c:\nodejs_workspace> node You will see the REPL Command prompt: > To terminate the Node REPL: Press ctrl-c twice.
12
Simple Expression Let’s try to use REPL to perform simple expressions:
c:\nodejs_workspace> node > 1 + 3 4 > 1 + (2 * 3) – 4 3 >
13
REPL variables If var keyword is not used then value is stored in the variable and printed. If var keyword is used then value is stored but not printed. You can use the above variables later. Use console.log to print anything. c:\nodejs_workspace> node > x = 10 10 > var y = 10 undefined > x + y 20 > console.log("Hello World!") Hello World!
14
Compound Statement (Statement Block)
Node REPL supports multiline expression (Compound statement) ({ }). c:\nodejs_workspace> node > var x = 0 undefined > while (x < 3) { … x++; … console.log("x: " + x); … } x: 1 x: 2 x: 3 >
15
Underscore variable Use _ to get the last result.
c:\nodejs_workspace> node > var x = 10 undefined > var y = 20 > x + y 30 > var sum = _ > console.log(sum)
16
REPL Commands Command Description ctrl + c
Terminate the current command ctrl + c twice Terminate the Node REPL ctrl + d up/down keys See command history and modify previous commands tab keys List of current commands .help List of all commends .break Exit from multiline expression .clear .save file_name.js Save current Node REPL session to a file .load file_name.js Load file content in current Node REPL session
17
Node.js - NPM Node Package Manger (NPM) provides two main functionalities: Online repositories for Node.js packages/modules. Command line utility to install packages, do version management and dependency management of Node.js packages. NOTE: NPM comes bundled with Node.js after v0.6.3 version. To verify the NPM version: C:\Nodejs_WorkSpace>npm --version (or npm –v)
18
NPM Global vs Local Installation
By default, npm installs any dependency in the local mode. Local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require. Globally installed packages/dependencies are stored in <user-directory>/npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any Node.js but can not be imported using require in Node application directly.
19
NPM Global vs Local (Linux platform)
In npm 1.0, there are two ways to install things: globally — This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied. locally — This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.
20
Global or Local? In general, the rule of thumb is:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
21
When You Cannot Choose You can do one of the following:
Install it in both places. (Recommended) Install it globally, and then link the two folders using symbolic links. Then you only need to update the global copy to update all the symlinks as well.
22
Install express module (local)
Install express, a popular web framework using local installation. c:\nodejs_workspace> npm install express Once npm completes the download, you can verify by looking at the content of c:\nodejs_workspace\node_modules Or type the following command: c:\nodejs_workspace> npm ls
23
Install express module (global)
Install express, a popular web framework using global installation. c:\nodejs_workspace> npm install express –g Once npm completes the download, you can verify by looking at the content of <user-directory>/npm/node_modules. NOTE: <user-directory> = c:\Users\my_uid\AppData\Roaming Or type the following command: c:\nodejs_workspace> npm ls -g
24
Install/uninstall a module
Installation of any module is as simple as typing the following command: c:\nodejs_workspace> npm install express Now you can use it in your js file as following: var express = require('express'); Use the following command to uninstall a module: c:\nodejs_workspace> npm uninstall express Note: to verify use the following command: c:\nodejs_workspace> npm ls
25
Update/search module Use the following command to update a module (run npm update in the same directory as your package.json file): c:\nodejs_workspace> npm update To test the update, run npm outdated. There should not be any results. NOTE: To update all global packages, you can use npm update -g. Use the following command to search a module: c:\nodejs_workspace> npm search express
26
Callbacks Concept Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node.js makes heavy use of callbacks. For example (non-blocking), a function to read a file may start reading file and return the control to execution environment immediately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for file I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.
27
How Node Application Work?
In Node Application, any asynchronous function accepts a callback as a last parameter and the callback function accepts error as a first parameter. var fs = require("fs"); fs.readFile('./test.txt', function(err, data) { if (err) { console.error(err); return; } console.log(data.toString()); }); console.log("Program Ended"); Here fs.readFile is a async function whose purpose is to read a file. If an error occur during read of file, then err object will contain the corresponding error else data will contain the contents of the file. readFile passes err and data to callback function after file read operation is complete. Note: console.error() will send output to stderr.
28
Event Loop Overview Node.js is a single threaded application but it supports concurrency via concept of event and callbacks. As every API of Node.js are asynchronous and being a single thread. It uses asynchronous function calls to maintain the concurrency. Node uses observer pattern (waiting for event). Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the even listener function to get executed. Event Loop process: (1) Load the program – (2) wait for event – (3) handle event – (4) execute callbacks – (5) exit if no more to do or wait for event [repeat from (2)]
29
Event Driven programming
Node.js uses Events heavily and it is also one of the reason why Node.js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for event to occur. What is the difference between Events and callbacks? The difference lies in the fact that callback functions are called when an asynchronous function returns its result where event handling works on the observer pattern. The functions which listens to events acts as observer. Whenever an event got fired, its listener function starts executing.
30
EventEmitter EventEmitter class lies in events module. It is accessibly via following syntax: // import events module var events = require ('events'); // create an eventEmitter object var eventEmitter = new events.EventEmitter(); When an EventEmitter instance faces any error, it emits an ‘error’ event. When new listener is added, ‘newListener’ event is fired and when a listener is removed, ‘removeListener’ event is fired. EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.
31
EventEmitter sample var events = require('events');
var eventEmitter = new events.EventEmitter(); var connected = function connected() { console.log('connection successful.'); eventEmitter.emit('data_receive'); } eventEmitter.on('connection', connected); eventEmitter.on('data_receive', function() { console.log('data received successfully.'); }); eventEmitter.emit('connection'); console.log('Program Ended.'); // output connection successful. data_received successfully. Program Ended.
32
EventEmitter Methods Method Description addListener(event, listener)
Adds a listener to the end of the listeners array for the specified event. Returns emitter, so calls can be chained. on(event, listener) once(event, listener) Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed. Returns emitter, so calls can be chained. removeListener(event, listener) It will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance. Returns emitter, so calls can be chained.
33
EventEmitter Methods Method Description setMaxListener(n)
By default EventEmitter will print a warning if more than 10 listeners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased . Set to zero for unlimited. listeners(event) Returns an array of listeners for the specified event. emit(eventName[, arg1][, arg2][,…]) Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each. Returns true if event had listeners, false otherwise.
34
Node.js - Streams What are Streams?
Streams are objects that let you read data from a source or write data to destination in continuous fashion. In Node, there are four types of streams: Readable: stream which is used for read operation. Writable: stream which is used for write operation. Duplex: stream which can be used for both read and write operation. Transform: a type of duplex stream where the output is computed based on input.
35
Streams and EventEmitter
Each type of stream from previous page is an EventEmitter and throws several events at times. For example, some of the commonly used events are: data: This event is fired when there is data is available to read. end: This event is fired when there is no more data to read. error: This event is fired when there is any error receiving or writing data. finish: This event is fired when all data has been flushed to underlying system.
36
Piping Streams Piping streams: Piping is a mechanism to connect output of one stream to another stream. It is normally used to get data from one stream and to pass output of that stream to another stream.
37
Node.js – File System fs module is used for File I/O.
fs module can be imported using following syntax: var fs = require('fs'); Synchronous vs Asynchronous: Every method in fs module have synchronous as well as asynchronous form. Asynchronous methods [readFile()] takes a last parameter [function (err, p2)] as completion function callback and first parameter of the callback function is error. It is preferred to use asynchronous method instead of synchronous method as former is non-blocking where later is blocking process. fs.readFile('test.txt', function(err, data) { });
38
Flags Flag Description r
Open file for reading. An exception occurs if the file does not exist. r+ Open file for reading and writing. An exception occurs if the file does not exist. w Open file for writing. The file is created ifitdoesnotexist or truncated ifitexists. w+ Open file for reading and writing. The file is created ifitdoesnotexist or truncated ifitexists. a Open file for appending. The file is created if it does not exist. a+ Open file for reading and appending. The file is created if it does not exist.
39
Node.js – File System Methods
fs method Description fs.close(fd, callback) Asynchronous close. No arguments other than a possible exception are given to the completion callback fs.ftruncate(fd, len, callback) Asynchronous ftruncate (remove data from the file). fs.mkdir(path [, mode], callback) Asynchronous mkdir. Mode defaults to 0777. fs.open(path, flags [,mode], callback) Asynchronous file open. fs.rmdir(path, callback) Asynchronous rmdir. fs.stat(path, callback) Asynchronous stat. The callback gets two arguments err, stats where stats is a fs.stats object. fs.unlink(path, callback) Asynchronous unlink (physically remove a file). fs.write(fd, buffer, offset, length, position, callback) Write buffer to the file specified by fd. Note: buffer can be Buffer or String; offset and length determine the part of the buffer to be written; position refers to the offset from the beginning of the file where this data should be written.
40
Node.js – Sync & Async Read
var fs = require('fs'); // Asynchronous read fs.readFile('./test.txt', function (err, data) { if (err) console.error(err); console.log('Asynchronous read: ' + data.toString()); }); // Synchronous read var data = fs.readFileSync('./test.txt'); console.log('Synchronous read: ' + data.toString()); console.log('Program Ended.');
41
Node.js – Sync & Async Write
var fs = require('fs'); var out_data = 'Outdata line 1. \r\nOutdata line 2. \r\nOutdata last line.'; // Asynchronous write fs.writeFile('./fs_write_output_async.txt', out_data, function (err) { if (err) console.log(err); console.log('Output Async file content: ' + out_data); }); // Synchronous write fs.writeFileSync('./fs_write_output_sync.txt', out_data); console.log('Output Sync file content: ' + out_data); console.log('Program Ended.');
42
Node.js Modules Module name Description buffer
buffer module can be used to create Buffer class. console console module is used to print information on stdout and stderr. dns dns module is used to do actual DNS lookup and underlying o/s name resolution functionalities. domain domain module provides way to handle multiple different I/O operations as a single group. fs fs module is used for File I/O. net net module provides servers and clients as streams. Acts as a network wrapper. os os module provides basic o/s related utility functions. path path module provides utilities for handling and transforming file paths. process process module is used to get information on current process.
43
Node.js – console module
console is a global object and is used to print to stdout and stderr. Method Description console.log([data][, …]) Prints to stdout with newline. This function can take multiple arguments in a printf-like way. console.info([data][,…]) Prints to stdout with newline. (support printf-like way). console.error([data][,…]) Prints to stderr with newline. (support printf-like way). console.warn([data][,…]) Prints to stderr with newine. (support printf-like way). console.time(label) Make a timer. console.timeEnd(label) Finish timer. Record output.
44
Node.js – process module
process is a global object and is used to represent Node process. Exit Codes: Node normally exit with a 0 status code when no more async operations are pending. There are other exit codes: Code Name Description 1 Uncaught Fatal Exception There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler. 2 Unused Reserved by Bash. 3 Internal JavaScript Parse Error The JavaScript source code internal in Node’s bootstrapping process caused a parse error. … >128 Signal Exits If Node receives a fatal signal such as SIGKILL or SIGHUP, then its exit code will be 128 plus the value of the signal code.
45
Node.js – process events
process is an eventEmitter and it emits the following events. Event Description exit Emitted when the process is about to exit. beforeExit This event is emitted when node empties it’s event loop and has nothing else to schedule. uncaughtException Emitted when an exception bubbles all the way back to the event loop. Signal Events Emitted when the processes receives a signal such as SIGNT, SIGHUP, etc.
46
Node.js – process properties
process provides many useful properties to get better control over system interactions. Property Description stdout A Writable Stream to stdout. stderr A Writable Stream to stderr. stdin A Writable Stream to stdin. argv An array containing the command line arguments. execPath This is the absolute pathname of the executable that started the process. env An object containing the user environment. exitCode A number which will be the process exit code. version A compiled-in property that exposes NODE-VERSION. platform What platform you are running on.
47
Node.js – process methods
process provides many useful methods to get better control over system interactions. Method Description abort() This causes node to emit an abort. This will cause node to exit and generate a core file. chdir(dir) Changes the current working directory of the process or throws an exception if that fails. cwd() Returns the current working directory of the process. exit(code) Ends the process with the specified code. If omitted, exit uses the “success” cdoe 0. uptime() Number of seconds Node has been running.
48
Node.js – os module os module is used for few basic operating-system related utility functions. os module can be imported using: var os = require('os'); Method Description os.tmpdir() Returns the O/S default directory for temp files. os.hostname Returns the hostname of the O/S. os.type() Returns the O/S name. os.platform() Returns the O/S platform. os.arch() Returns the O/S CPU architecture. Possible values are x64, arm, and ia32. os.release() Returns the O/S release. os.totalmem() Returns the total amount of system memory in bytes. os.freemem() Returns the amount of free system memory in bytes. os.networkinterfaces() Get a list of network interfaces.
49
Node.js – os property Property Description os.EOL
A constant defining the appropriate End-of-line marker for the operating system.
50
Node.js – net module net module is used to create both servers and clients. It provides an asynchronous network wrapper. net module can be imported using: var net = require("net"); Method Description net.createServer([options][, connectionListener]) Creates a new TCP server. The connectionListener argument is aumatically set as a listener for the ‘connection’ event. net.connect(port[, host][, connectionListener]) Creates a TCP connection to port on host. If host is omitted, ‘localhost’ will be assumed. The connectionListener parameter will be added as an listener for the ‘connect’ event. It is a factory method which returns a new ‘net.Socket’. net.createConnection(port[, host][, connectionListener])
51
Node.js – net.Server class
net.Server class is used to create a TCP or local server. var net = require('net'); var server = net.createServer(function (socket) { socket.end('Socket End.'); }); Method Description server.listen(port[, host][, callback] Begin accepting connections on the specified port and host. A port value of zero will assign a random port. server.close(callback) Finally closed when all connections are ended and the server emits a ‘close’ event. server.address() Returns the bound address, the address family name and port of the server as reported by the operating system.
52
Node.js – other modules path module is used for handling and transforming file paths. Path module can be imported using: var path = require("path"); dns module is used to do actual DNS lookup as well as to use underlying operating system name resolution functionalities. var dns = require("dns"); domain module is used to intercept unhandled error. var domain = require("domain");
53
Node.js – Web Module
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.