0% found this document useful (0 votes)
974 views

NODEjs lecture 1

Uploaded by

smeet l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
974 views

NODEjs lecture 1

Uploaded by

smeet l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

NODE JS

Introduction
Jalpa Mehta
Assistant Professor
SAKEC,Chembur

Module:5
• Contents
Environment set up
• First App
• Asynchronous Programming
• Call back concept
• Event loops
• REPL
• Event Emitter
• Networking Module
• Buffers
• Streams
• File Systems
• Web Modules
• Node.js with mongodb
Full stack JavaScript tools
• Using Node.js for backend, you automatically get all the pros
of full stack JavaScript development, such as:
• better efficiency and overall developer productivity
• code sharing and reuse
• speed and performance
• easy knowledge sharing within a team
• a huge number of free tools
Environment Set UP
• If you want to set up your environment for Node.js, you need to have the
following two software on your computer,
(a)Text Editor : Examples of text editors include Windows Notepad, OS Edit
command, Brief, Epsilon, EMACS, and vim or vi.

(b) the Node.js binary installables : Node.js distribution comes as a binary


installable for SunOS, Linux, Mac OS X, and Windows operating systems with
the 32-bit (386) and 64-bit (amd64) x86 processor architectures

•Download the latest version of Node.js installable archive file from


•https://nodejs.org/en/download/
FIRST APP
• A Node.js application consists of the following three important
components −
• Import required modules − We use the require directive to load
Node.js modules.
• Create server − A server which will listen to client's requests similar to
Apache HTTP Server.
• Read request and return response − The server created in an earlier
step will read the HTTP request made by the client which can be a
browser or a console and return the response.
• Creating Node.js Application
• Step 1 - Import Required Module
• We use the require directive to load the http module and store the
returned HTTP instance into an http variable as follows −
• Step 2 - Create Server
• We use the created http instance and
call http.createServer() method to create a server instance and
then we bind it at port 8080 using the listen method
associated with the server instance. Pass it a function with
parameters request and response. Write the sample
implementation to always return "Hello World"
• Step 3 - Testing Request & Respons
• Execute the .js file using
➢node filename.js
➢Open it on browser

➢See example myfirst.js given in the notes.


Node.js repl
• The term REPL stands for Read Eval Print and Loop. It specifies a
compute environment like a window console or a Unix/Linux shell
where you can enter the commands and the system responds with
an output in an interactive mode.
• REPL Environment
• The Node.js or node come bundled with REPL environment. Each
part of the REPL environment has a specific work.
• Read: It reads user's input; parse the input into JavaScript data-
structure and stores in memory.
• Eval: It takes and evaluates the data structure.
• Print: It prints the result.
• Loop: It loops the above command until user press ctrl-c twice.
Node.js repl
• Starting REPL
REPL can be started by simply running node on shell/console
without any arguments as follows.
> node
> node
>1+3

Use Variables
You can make use variables to store values and print later like any
conventional script. If var keyword is not used, then the value is
stored in the variable and printed.
Whereas if var keyword is used, then the value is stored but not
printed. You can print variables using
console.log()
• > node
> x = 10
10
> var y = 10
undefined
>x+y
20
> console.log("Hello World")
Hello World
undefined
Multiline Expression
Node REPL supports multiline expression similar to JavaScript. Let's
check the following dowhile loop in action:
> node
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... } while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
NODe.js package
Manager(NPM)
• Node Package Manager (NPM) provides two main
functionalities −
• Online repositories for node.js packages/modules which are
searchable on search.nodejs.org
• Command line utility to install Node.js packages, do version
management and dependency management of Node.js
packages.

•NPM comes bundled with Node.js installables after v0.6.3


version. To verify the same, open console and type the following
command and see the result −

•$ npm -- version
Parts of node.js

You might also like