lect06_NodeJS_1
lect06_NodeJS_1
Lecture 6
Intro to Node.js & Express.js
Today’s Contents
● Introduction to Node.js
● Parameters in Node
● Introducing Express.js
● HTTP Errors
Review: Web Service
Web service: software functionality that can be invoked through the Internet using
common protocols (by contacting a program on a web server).
● Web services can be written in a variety of languages
● Many web services accept parameters and produce results
● Clients contact the server through the browser using XML over HTTP and/or AJAX
Fetch code
● The service's output might be HTML but could be text, XML, JSON, or other content
How Does a Web Service Respond to Requests?
Why Do We Need a Server to Handle Web Service Requests?
Web servers contain software to run those programs and send back their output.
Client-side vs. Server-side JavaScript
Client Server
API
Browser Node.js
● So far, we have used JS on the browser (client) to add interactivity to our web
pages
● "Under the hood", your browser requests the JS (and other files) from a URL
resource, loads the text file of the JS, and interprets it realtime in order to
define how the web page behaves.
● In Chrome, it does this using the V8 JavaScript engine, which is an open-
source JS interpreter made by Google. Other browsers have different JS
engines (e.g. Firefox uses SpiderMonkey).
● Besides the standard JS language features, you also have access to the DOM
when running JS on the browser - this includes the window and document
objects.
Node.js: Server-side JS
● When using Node, you do not have access to the browser objects/functions
(e.g. document, window, addEventListener, DOM nodes).
● Instead, you have access to functionality for managing HTTP requests, file i/o,
and database interaction.
● This functionality is key to building REST APIs!
Client-side vs. Server-side JavaScript
Client-side Server-side
● Adheres (mostly) to the ECMAScript ● Adheres (mostly) to the ECMAScript
standards standards
● Parsed and run by a browser and therefore ● Parsed and run by Node.js and therefore
has access to document, window, and has access to File I/O, databases, and
more more
● Calls APIs using fetch, which sends HTTP ● Handles requests from clients, sending
requests to a server HTTP responses back
● Runs only while the web page is open
● Runs as long as Node is running and
● Handles "events" like users clicking on a
listening
button
● Handles HTTP requests (GET/POST etc)
JavaScript Template Literals
● Template literals provide an easy way to embed variables and expressions into a
string
● With this syntax: ${<variable/expression>}
● You can create long HTML strings in your JavaScript code and embed variables
and expressions in them.
● This often proves useful when manipulating HTML with JavaScript.
When you have Node installed, you can run it immediately in the command line.
1. Start an interactive REPL with node (no arguments). This REPL is much like
the Chrome browser's JS console tab.
a. ("REPL" stands for "Read Evaluate Print Loop". It's a kind of tool that
allows you to run one line of code at a time. REPL-style tools exist in
most languages.)
2. Execute a JS program in the current directory with node file.js
Getting started with Node.js
node command
Running node without a filename runs a REPL loop
- Similar to the JavaScript console in Chrome, or when you run "python“
$ node
> let x = 5;
undefined
> x++
5
> x
6
Getting started with Node.js
Node can be used for executing scripts written in JavaScript, completely unrelated
to servers
The NodeJS require() statement loads a module, similar to import in Java, python or
include in C++.
- We can require() modules included with NodeJS, or modules we've written ourselves.
- In this example, 'http' is referring to the HTTP NodeJS module
The http variable returned by require('http') can be used to make calls to the
HTTP API:
The request event is emitted each time there is a new HTTP request for the
NodeJS program to process.
Emitter .on
server.on('request', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type',
'text/html');
res.end('<h1>Hello, World!</h1>');
});
The req parameter gives information about the incoming request, and the res
parameter is the response parameter that we write to via method calls.
statusCode: Sets the HTTP status code.
setHeader(): Sets the HTTP headers.
end(): Writes the message to the response body then signals to the server that the message is
complete
Template literals
listen(): Starts the server and listens for requests on the specified
port.
You can think of the port as a house number that, together with the
server's IP address, uniquely identifies the server's address.
Popular default ports
There are many well-known ports, i.e. the ports that will be used by
default for particular protocols
Running the server
The process does not end after we run the command, as it is now waiting for HTTP
requests on port 3000.
Server response
What is REST architecture?
2. A REST Server provides access to resources and REST client accesses and
modifies the resources using HTTP protocol. Here each resource is identified
by URIs/ global IDs. REST uses various representation to represent a resource
like text, JSON, XML but JSON is the most popular one.
HTTP methods
Following four HTTP methods are commonly used in REST based architecture.
1. GET Method
The purpose of the GET operation is to retrieve an existing resource on the server
and return its XML/JSON representation as the response. It corresponds to the
READ part in the CRUD (CREATE, RETRIEVE, UPDATE and DELETE) term.
Example:
Retrieve all users from the database.
Retrieve details of a specific user by their ID.
HTTP methods
Example of the GET method using Node.js to implement.
server.listen(3000, () => {
console.log('Server running on port 3000');
});
The server checks if the incoming request is a GET method and matches the /users
endpoint.
HTTP methods
2. POST Method
The POST verb in the HTTP request indicates that a new resource is to be created
on the server. It corresponds to the CREATE operation in the CRUD term.
3. PUT Method
● Example:
○ Updating a User’s Information
HTTP methods
Example of the PUT method using Node.js to implement.
const server = http.createServer((req, res) => {
if (req.method === 'PUT' && req.url.startsWith('/users/')) {
const id = req.url.split('/')[2];
let body = …….
req.on('end', () => {
const { name, email } = JSON.parse(body);
const user = users.find(user => user.id === parseInt(id));
if (user) {
user.name = name || user.name;
user.email = email || user.email;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(user));
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'User not found' }));
}
})}
});
HTTP methods
4. DELETE Method
There are a few steps to starting a Node.js application, but luckily most projects will follow the
same structure.
When using Node.js, you will mostly be using the command line
Along the way, a tool called npm will help install and manage packages that are useful in your
Node app.
Starting a Node.js Project (ExpressJS)
Or better, run npm install -s express to save the express package into
the dependencies list under package.json
Starting app.js
"use strict";
app.listen(8080);
Node.js Modules
When you run a .js file using Node.js, you have access to default functions in JS
(e.g. console.log)
In order to get functionality like file i/o or handling network requests, you need to
import that functionality from modules - this is similar to the import keyword you
have used in Java or Python.
In Node.js, you do this by using the require() function, passing the string name
of the module you want to import.
For example, the module we'll use to respond to HTTP requests in Node.js is called
express. You can import it like this:
Using const to declare a variable inside of JS just means that you can never
change what that variable references. We've used this to represent "program
constants" indicated by ALL_UPPERCASE naming conventions
For example, the following code would not work:
const specialNumber = 1;
specialNumber = 2; // TypeError: Assignment to constant variable.
● app.get allows us to create a GET endpoint. It takes two arguments: The endpoint URL path, and
a callback function for modifying/sending the response.
● req is the request object, and holds items like the request parameters.
● res is the response object, and has methods to send data to the client.
● res.set(...) sets header data, like "content-type". Always set either "text/plain" or
"application/json" with your response.
● res.send(response) returns the response as HTML text to the client.
● res.json(response) does the same, but with a JSON object.
When adding a route to the path, you will retrieve information from the request, and send back a
response using res (e.g. setting the status code, content-type, etc.)
If the visited endpoint has no matching route in your Express app, the response will be a 404 (resource
not found)
Useful Request Properties/Methods
Name Description
Name Description
res.send() Sends information back (default text with HTML content type)
res.sendStatus() Sets the response status code with the default status text, and
sends response back
Setting the Content Type
By default, the content type of a response is HTML - we will only be sending plain text or JSON
responses though in our web services
To change the content type, you can use the res.set function, which is used to set response
header information (e.g. content type).
You can alternatively uses res.type("text") and res.type("json") which are equivalent
to setting text/plain and application/json Content-Type headers, respectively.
These are attached to the request object and can be accessed with req.params
Unlike path parameters, these are not included in the path string (which are matched using
Express routes) and we can't be certain that the accessed query key exists.
If the route requires the parameter but is missing, you should send an error to the client in the
response.
Servers, clients, localhost
This is just like what we're looking at with Node on our localhost.
So, previously we talked about why. Let's look at what and how.
Choosing Error Codes
Use 500 (Server error) status codes for errors that are independent of any client
input.
● Errors caught in Node modules that are not related to any request parameters
● SQL Database connection errors (next week!)
● Other mysterious server errors...
Setting Errors
The Response object has a status function which takes a status code as an
argument.
The 400 status code is what we'll use to send back an error indicating to the
client that they made an invalid request.
A helpful message should always be sent with the error.
app.get("/cityInfo", function (req, res) {
let state = req.query.state;
let city = req.query.city;
if (!(state && city)) {
res.status(400).send("Error: Missing required city and state
query parameters.");
} else {
res.send("You sent a request for " + city + ", " + state);
}
});