COMP5347: Web Application Development Server-side Development Node.js
Dr. Basem Suleiman
School of Computer Science
The University of Sydney
Page 1
Outline
– How web server works – Forms
– HTTP GET. POST
– Node.js Execution
– Node.js Application Structure
– Express.js Basics
– Routing
– Middleware
– Template Engines
The University of Sydney
COMP5347 Web Application Development
Page 2
•
Server-side Development
Server-side development involves the use of programming technology to create script that dynamically generate content
Execution of Client-side script (a) and server-side script (b)
Server-side development technologies:
– Java Server Pages
– Active Server Pages (.NET)
– PHP
– Node.js
– Ruby on Rails
– Python
•
The University of Sydney
COMP5347 Web Application Development
Page 3
• •
Server-side Development
Server-side script: software running on a server and uses HTTP request-response loop for interaction with the clients
A server-side script can access resources made available by the server
The University of Sydney
COMP5347 Web Application Development
Page 4
Common Server Types
– Web Servers
– A computer that runs a Web server software (e.g., Apache, MS IIS) and service
HTTP requests
– Application Servers
– A computer that hosts and execute Web applications developed in a certain technology; PHP, Ruby on Rails
– Database Server
– A computer that is devoted for running a DBMS such as MySQL, SQL server
– Web servers must choose an application stack (OS, Web server software, database and scripting language for dynamic requests)
– LAMP: Linux, Apache, MySQL and PHP
– MEAN: MongoDB, Express.js, Angular.js, Node.js
– Server software should be installed and configured on the Server
– N-tier architecture (physical/logical)
The University of Sydney
COMP5347 Web Application Development
Page 5
•
How server works – URLs
Most basic web server acts like a file system while file requests are sent using HTTP protocol
– Path: similar to a computer file system. The root of a web server correspond to a folder somewhere on the server.
– Linux servers the path is /var/www/html
The University of Sydney
COMP5347 Web Application Development
Page 6
Internet Protocols – HTTP
– Transport layer protocol (e.g. TCP) provides logical communication between processes running on the hosts identified by ip:port
– In particular, TCP ensures that transmissions arrive, in order, and without error
– Application layer protocol (e.g. HTTP) defines the syntax and semantics of the message send between processes
The University of Sydney
Page 7
HTTP Headers
• Request headers include data about the client machine.
• Response headers have information about the server answering the request
and the data being sent
The University of Sydney
Page 8
COMP5347 Web Application Development
Request/Response message
HTTP request line
HTTP response status
The University of Sydney
Page 9
COMP5347 Web Application Development
Request Methods
HTTP/1.0
– GET
– POST
– HEAD
– asks server to leave requested
object out of response
HTTP/1.1
– GET, POST, HEAD – PUT
– uploads file in entity body to path specified in URL field
– DELETE
– deletes file specified in the URL
field
The University of Sydney
COMP5347 Web Application Development
Page 10
HTTP Response Status Code
– 3 digit response code
– 1XX–informational
– 2XX–success
• 200OK
– 3XX–redirection
• 301 Moved Permanently
• 302 Found/Moved temporary • 303 Moved Temporarily
• 304 Not Modified
– 4XX – client error • 404 Not Found
– 5XX–servererror
• 505 HTTP Version Not Supported
The University of Sydney
Page 11
COMP5347 Web Application Development
HTTP Request Processing
– Address Resolution – Simpleexample
• A file stored under the absolute path /usr/mit/yourlogin/lib/html/week2.html
• Is accessible through this URL: http://www.it.usyd.edu.au/~yourlogin/week2.html
– Apache administrator has set the mapping between URL path info and local file system path info
• /~yourlogin/week2.htm is mapped to /usr/mit/yourlogin/lib/html/week2.html in local file system
The University of Sydney
COMP5347 Web Application Development
Page 12
Static and Dynamic Content
– Static content stored in local file system
– Static content page: HTML page, plain text, image files, etc
• Use the predefined mapping to locate the file
• Construct HTTP response with header information
– As-is page: static file containing complete HTTP responses
• No response construction is required • Indicate is-as file (e.g., extension)
– Dynamic contents request explicit server side programmatic action to generate response
– PHP, Perl scripts
– Application server
The University of Sydney
COMP5347 Web Application Development
Page 13
Server-Side Program Forms
– As a “script”
– CGI scripts, or Java Servlets, or JavaScript
– Just like a regular program
– Parserequestusingregularexpression
– Write (HTML) response using stream operations provided by the language
– As a Server Page (template processing)
– PHP, ASP, JSP
– Program is structured around the returning text page
– Script code are inserted into the HTML code to execute at certain points
– Combination based on MVC
The University of Sydney
COMP5347 Web Application Development
Page 15
Server-Side Program
– Basic processing steps
– Parse HTTP request to obtain information carried in the request • Additional parameter, client restriction, cookie and so on
– Processing the HTTP request information
– Generate response
– “script” style program regardless of the language chosen
– They are hidden in “server page” style program
– Supporting/external services for
– networking: e.g. sending and receiving request/response
– Common processing: e.g. extracting header information from both request and response messages
– Supported by server (e.g., application server) or language framework
The University of Sydney
COMP5347 Web Application Development
Page 16
Sending data to server
The University of Sydney
COMP5347 Web Application Development
Page 17
HTML Forms – Query Strings
How the browser sends the data to the server
• Through HTTP requests
• The browser packages user’s data into a query string
• Query string: a series of name=value pairs separated by &
• HTML form element’s name attribute
• User input data
The University of Sydney
Page 18