AJAX / XHR states / CORS / Data access
Tasks
- Create a simple HTML page with an info text field and a single button
- Implement a JavaScript function which is triggered when the button is clicked
- The function should fetch relatively large file (e.g. 100-200MB)
- in the text field show following states:
- loading - when the open method was called
- loaded - when the send method was called
- downloading - while the data is being downloaded
- finished downloading - when the data has beeen downloaded
- you can use Promise, async/await
Description
AJAX overview:
- Asynchronous JavaScript and XML
- technique for creating better, faster, and more interactive web applications
- relies on XML, JSON, HTML, CSS and JavaScript
- AJAX is not a programming language
Running this demo by using the jquery module to achieve the ajax request. Following the xhr state in https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState. The browser console would show the current XMLHttpRequest state when starting the server.
Deal with the problem of CORS
To deal with the CORS problem in the ajax request, I upload the large file to the Amazon S3 bucket and set the following json code to allow the local server accessing the remote resources.
Data access
A company needs to design an AJAX aplication that will access various resources on the Web by using JavaScript code running in a browser. This application is not public and only internal employees of the company can use it. This application will be available at the address http://company.at. Following table shows a list of URL addresses of resources, their formats and HTTP methods that the application will use to access these resources. Add all possible access technologies to the table for methods GET and DELETE. Note: parameter {dob} means date of birth.
Resource | Format | GET | DELETE |
---|---|---|---|
http://company.at/customers | XML | AJAX (1) | AJAX (2) |
http://company.at/suppliers | JSON | AJAX, JSONP (3) | AJAX (4) |
http://weather.at/innsbruck | XML | AJAX-CORS (5) | AJAX-CORS (6) |
http://people.at/students | JSON | AJAX-CORS, JSONP (7) | AJAX-CORS (8) |
http://people.at/{dob}/contact | VCARD | AJAX-CORS (9) | AJAX-CORS (10) |
- (1) (2): Follow the Same Origin Policy
- (3) (4): JSONP only works on GET method
- (5) (6) (9) (10): Using CORS to deal with problem of different domain
- (7) (8): JSONP only works on GET method
- AJAX-CORS (add http header in server)
|
|
- AJAX-CORS (Using CORS Anywhere which adds CORS headers to the proxied request)
Code (index.html)
|
|
Node.js server in Docker
Tasks
- Implement a simple HTTP server in Node.js
- implement a “hello NAME” service endpoint
- request: “http://localhost:8080/John”, response “Hello John”
- Define an image Dockerfile, with the following specifications:
- build on a chosen node.js image
- the container should listen on port 8888
- load the server implementation from a local directory
- run the server
- Create a docker image from the Dockerfile
- Create a container for the image
- tip: use the -p parameter to map public port to a private port inside the container (e.g. -p 8080:8888)
- Check if the container is running (docker container ls)
- Send several requests to the server running within the container
- you can use ping, curl or telnet for testing
- Stop the container
Description
Docker Overview:
- package an application and its dependencies in a virtual container
- build, commit and share images
- based and primarily for Linux
- allows operating-system-level virtualization
- run isolated packages, also known, as containers
- Define an image Dockerfile and create a docker image
|
|
|
|
- Create a container and run the curl. -p (Port mapping for the container and the host machine)
|
|
Docker advanced (Redis)
Tasks
- Task 1: Start a docker container for a redis server:
- build on a chosen redis image
redis
- run the server
- build on a chosen redis image
- Task 2: Start a docker container for a redis client:
- build on a chosen redis image
redis
- with the client insert some user info where key is the person name, and value is the address
- build on a chosen redis image
- Task 3: Implement a simple HTTP server in Node.js
- implement a “http://localhost:8080/person/{person_name}/address” API, which returns the address of a person
- request: GET “http://localhost:8080/person/John/address”, response “Thakurova 9, 160 00, Prague”
- the server should fetch the data from a Redis server. Redis runs in a separate container than the node.js server! (see above).
- Task 4: Define an image Dockerfile (for the node.js server), with the following specifications:
- build on a chosen node.js image
- load the server implementation from a local directory
- run the server
- Create a docker image from the Dockerfile
- Create and run a container
- Test the server - it shoudl return the address for a person retrieved from the linked redis server container
Description
Task1 - Run the Redis Server in a container Redis-server stores the data from Redis-client, and can handle the request from the Node.js
|
|
Task 2 - Run the Redis client in a container Redis-client is linked to the Redis-server
|
|
Redis-client let user insert the data {kay,value}
Task3 - Implement a simple HTTP server and run it on the host, and Node.js fetches the data from the Redis-server
Must use redis version 3.0.2!!!!!!!!!
|
|
Task4 - Docker-compose file
|
|
Code (Redis server)
|
|
HTTP/2 Push mechanism
Tasks
- Implement an HTTP/2 push mechanism
- Implement a simple HTML page consisting of at least 3 additional resources (e.g. css, js, image, ..)
- Implement a simple http server serving the page via HTTP/2
- When the html page is requested, push all those files together with the requested page
- Use http2 module
Description
HTTP/2 with the push method
HTTP/2 without the push method
The above results indicate that HTTP/2 using the push method is faster than the method without the push method. The key reason is the concurrency in the different types of files in one connection (Extend this concept to the QUIC protocol, fixing the HoL blocking problem).
Code
|
|
Server-Sent Events
Task
Design and implement simple SSE server in Node.js and simple client in HTML/JavaScript.
- The client connects to server and displays all incoming messages.
- The server sends every 2 seconds one message - e.g. one line of text file.
Description
- Server-sent events are functions extended on top of HTTP.
- The server can actively send data to the client based on the new MIME type( text/event-stream ).
Client: The URL is passed to EventSource() and creates a new EventSource object. onmessage() is triggered by receiving the data.
Server: new MIME type(text/event-stream).
Code
|
|
retry: Specify the time interval for the browser to re-initiate the connection
id: ID for each event-stream
data: the event data
‘\n’ gaps the indicator. ‘\n\n’ switches the lines
OAuth-Browser-Based-App
Task
Design and implement a simple OAuth - Browser-Based App. Browser-based apps run entirely in the browser after loading the source code from a web page.
- Use a simple server (https) for serving static content (html, js, …).
- Configure any OAuth application of your choice.
- You can use any OAuth solution as authorization and resource server: Google, GitHub, …
- The app in browser connects to the authorization server and allows access to the resources.
- The app collects an presents in the browser any resource from the resource server using the provided code/token (e.g. list of contacts, files, messages, repositories, …)
- Do not use any OAuth library (e.g. GoogleAuth, …)
Description
Workflow:
-
Log in to the Google cloud console to register a new project. Get the client_id and client_secret and set the redirect_link
-
Client calls getGoogleAuthURL() to get the code from https://accounts.google.com/o/oauth2/v2/auth
Scope type: userinfo is the public data. If we want to leverage the common services, just like Google drive or photo…, we need to add the test users until publishing the project.
-
The server posts the code with Axios to get the token from the https://oauth2.googleapis.com/token
-
Call jwt.decode(token) to get the data
-
Create a new project in the Google cloud console
-
Enter index.html to log in through ouath2
After logging, Authorization: OK. Print the callback data
Code (OAuth server implement)
|
|