COMP 1537 - Week 6 - Intro To Node
COMP 1537 - Week 6 - Intro To Node
What is it?
A (mostly) server-side run-time environment for web content delivery
It uses an "event-driven architecture"
Fancy way of saying that it responds to events – just like a Graphical User Interface (GUI)
Ryan Dahl wrote it in 2009
He used "Google's V8 JavaScript engine, an event loop, and a low-level I/O API"
Lotsa modules!
Via Node Package Manager (NPM) – see: https://www.npmjs.com/
Non-blocking functions
A call is made and the main execution returns immediately
We’ll look at what this means in a bit …
Non-blocking I/O operations allow a single process to serve multiple requests at the same time.
Instead of the process being blocked and waiting for I/O operations to complete, the I/O operations
are delegated to the system, so that the process can execute the next pieceCopyright
of code. Ⓒ 2022, Arron Ferguson
HOW NODE WORKS (1/2)
Event driven
The server waits for requests, doing nothing until a request is made
This is not uncommon to GUIs such as iOS, Android, and any desktop OS
Groupon
LinkedIn
Medium
Netflix
PayPal
Trello
Uber
Walmart
What is “require”?
A function that we can call
It gets other JS modules that have been written
It can be used anywhere in your code – not just at the top of your code file
Unlike a Java import, or using in C# - require is not a directive, it’s a function
Require executes the JS code
The JS code is treated as an object – which may have other objects & functions attached to it
require is not a directive its a call to function and we can put it everywhere but we should put it on top for readability
require is basically a search function that searches the input and finds the module/package with that name and returns it
express returns something called app
Paths are like entrances to resources that we wish to share with users
E.g., image galleries, customer data, video streams, etc.
Paths are just logical/hierarchical hooks to back-end logic that we wish to invoke
Based on an assigned meaning that we give it
The callback function accepts two parameters – we’ve called them ‘req’ & ‘res’
First argument represents the request from the client (e.g., web browser)
Second argument represents the response we will send to the client (e.g., web browser)
Request object methods/properties:
Data sent with the request, the properties of the browser connecting
Response object method/properties:
Data we want to send back to the requestor, meta information about the response
The request-response pair argument is a pattern found on backend/server-side
programming, including:
Java, C#/.NET
HTTP has a few methods we can use to talk to the server with
Methods are ways of stating intention to communicate – protocols
What’s a protocol?
Examples of real life protocols:
COMP 1537 assignments are due by a certain date & time, if you submit an assignment that is
late, you get zero marks for the assignment
A doctor’s office requires 24 hours notice of cancellation, otherwise you have to pay for the
cancelled appointment
In the HTTP protocol there are two more commonly used methods:
GET
POST
GET:
Retrieve a resource, can be cached, request in browser history
Can be bookmarked, never used for sensitive data
Should not be used for something that causes side-effects
POST:
Submits data to be processed, data is included in body of request
Never cached, cannot be bookmarked, do not remain in browser history
May include the creation of a new resource or update of an existing resource
Or both
No restrictions on data length
For now we will focus on HTTP GET
What is Express.js?
A lightweight routing framework for Node.js – it’s downloadable as a module!
How to get it?
Use command line: npm install -g express
‘-g’ means global – you may have to have admin privileges on your computer for this!
But what about just retrieving files from the file system? Like how Apache Web
server does?
The virtual path presented to users on the web app (seen in the web browser)
Virtual because there is no actual directory/folder path in the file system that matches
this pattern
The actual (static) path that is found in the file system of your computer
To serve static files such as images, CSS files, and JavaScript files, use the express.static
built-in middleware function in Express.
app.use("/js", express.static("./public/js"));
app.use("/css", express.static("./public/css"));
app.use("/img", express.static("./public/img"));
The app. use() method puts the specified middleware functions at the specified path
Copyright Ⓒ 2022, Arron Ferguson
Virtual paths are virtual file system paths that map to a physical path on your domain and have their own set of permissions.
Basic commands:
npm install – install the dependencies in the local node_modules folder
npm update - update all the packages listed to the latest version
https://docs.npmjs.com/cli/install
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction
https://nodejs.org/en/
https://expressjs.com/
https://www.npmjs.com/
https://nodejs.org/api/globals.html