NODE.
JS Module 2
WHAT IS [Link]?
• Web application framework for [Link]
• Light-weight and minimalist
• Provides boilerplate structure & organization for your web-
apps
INSTALLING [Link]
• Install node and node package manager (npm)
– ‘sudo apt-get install nodejs’ on
Debian/Ubuntu.
– Installer available for Mac [1]
– Installing node should also install npm
– Running ‘node’ on a shell would now
open an interactive node session (just
like python)
[1] [Link]
Installing [Link]
• We can now create a project (say a blog),
initialize it as a node package & install [Link]
– ‘mkdir blog && cd blog’
– ‘npm init’
• Creates a node package (touching [Link])
• [Link]: lists all dependencies (node modules)
– ‘npm install express --save‘
• Install express and add it as a dependency to our app
Hello World - Example
Let’s write a simple hello world program using node and express.
We create a file ‘[Link]’ inside the blog directory, with the following
contents:
• The app is invoked with ‘node [Link]’ on the console. At this point, node listens
on port 3000, but doesn’t really do much.
• We can check this out, by accessing [Link] on the browser.
Example - Continued
With a few more lines:
• Declare an instance of express called ‘app’
• Accept requests on ‘/’ (the root path on the URL – localhost:3000) and
pass it to function (more on this later).
• The processing function or ‘callback’ receives the request and is expected
to present with a response.
Example - Continued
With a few more lines:
• Send a string as the response.
Example - Continued
We can obviously add some ‘dynamically’ generated content:
Routing
• Apps needs to know what exactly to do when
a request is made to a particular endpoint.
– [Link] -- ‘/’ is the endpoint
– [Link] -- ‘/admin’ is the
endpoint
• We do this by defining routes. In the previous
example, we defined one route ‘/’ which
returned a string as the response.
Routing - structure
• [Link](ENDPOINT, HANDLER)
– Equivalent to [Link](where, what-to-do)
• METHOD: What kind of request is the route for? Most common ones
are ‘GET’/’POST’ but express supports a bunch2
– Another generic method called ‘all’ (covered next
week).
[2]: [Link]
Routing - structure
• [Link](ENDPOINT, HANDLER)
– Equivalent to [Link](where, what-to-do)
• ENDPOINT: What path is the request directed to.
– This can be a regular expression.
– [Link]('/ab*cd', …..); will match:
• localhost:3000/abcd, localhost:3000/abXYZcd and so
• on.
Routing - structure
• [Link](ENDPOINT, HANDLER)
– Equivalent to [Link](where, what-to-do)
• ENDPOINT: What path is the request directed
to.
– This can be a regular expression.
– [Link](/.*cat$/, …..); will match:
• localhost:3000/happycat, localhost:3000/sadcat and so
on.
Routing - structure
• [Link](ENDPOINT, HANDLER)
– Equivalent to [Link](where, what-to-do)
• HANDLER: Or a ‘callback’ – function that
determines how to process the request.
– Takes in a few parameters (request and response
as an example).
Routing - structure
• Handler:
– Sent in an anonymous function that would receive
the request and return a response.
– Data pertaining to this instance of the request can
be obtained through the ‘request’ variable.
– Data pertaining to the response should be
handled through the ‘response’
variable.
Routing - structure
• Handler:
– Handler functions can also be passed as variables
for brevity.
Routing – chaining handlers
• Handlers can be chained together.
– Route handlers take a third parameter called ‘next’.
Invoking this variable as a function, delegates the request
to the next handler (if there is one).
Routing – chaining handlers
– Ordering of the handlers matter.
– If secret_check() sent in a response (instead of calling
next) the delegation to error() won’t happen and the
handler chain would stop with secret_check()
Response methods
Aside: Logging
• ‘[Link](…)’ is extremely useful for logging
information server side (by default would
print out to the console).
– Helps with debugging.
– Prints out the client’s IP address for every request
they make -- to the console (where you invoked
‘node [Link]’)
Express generator
• Express provides a tool that can create and
initialize an application skeleton.
– Sets up a directory structure for isolating your
business and presentation logic (among
others).
– Not a norm in any way, can be used as a starting
point.
Express generator
• How to use it?
– Installing using ‘npm install express-generator –g’
– Can then be invoked with ‘express’
• Create an app (for instance, a blog)
– ‘express blog’ – will generate the directories/files
– ‘cd blog && npm install’ – will install the dependencies
– Invoke the server using ‘DEBUG=blog ./bin/www’
Express generator
• Provides & sets up some boilerplate
code for getting started faster
• Adds some commonly used
dependencies
• Pretty good starting point for digging
deeper into [Link]/[Link]
• Creates a structure that allows
separation of concerns on
business/presentation logic (more on
this next week)
Resources
• [Link] (Sections: Guide, API ref.)
• [Link]
• [Link]
net-33367
• Some resources on the slides are based on ‘Getting Started’
section from [Link]