Source >> https://fullstackopen.com/en/#course-contents
Links >> [Notes on: Eloquent JavaScript]({{< relref “eloquent-javascript” >}})
{{< toc >}}
Fundamentals of Web Apps
Traditional Web Applications
Most of the code below represents bad practice nowadays.
This is the code running on this website. It dynamically generates the HTML code based on the (changing) number of notes.
Going to the notes page, we
find out that the head-section of the HTML contains a script-tag, which makes
your browser load a JavaScript file called main.js
:
Document Object Model (DOM)
We can think of HTML-pages as tree structures. In the case of dynamic webpages, DOM is an Application Programming Interface (API) that acts as an interface between JavaScript and the object-oriented document (of which the browser renders a HTML representation) itself. Within a webpage, JavaScript can:
- add, change, and remove any of the HTML elements and attributes
- change any of the CSS styles
- react to all the existing events
- create new events
Manipulate the document-object from console
Turns out that we can manipulate the object from the browser console:
Transclude of 2020-06-05_09-36-20_screenshot.png"
The newly created note will render but disappear on a reload of the page since we have not pushed the change to the server yet.
Cascading Style Sheets (CSS)
CSS is a markup language that determines the style of a webpage. The main.css
file that is loaded as a stylesheet in the HTML on our example page defines two
class selectors. Class selectors match elements based on the contents of their
class attributes. Here is a general example followed by the main.css
file.
There are also other attributes than class
. For example, there is the id
attribute which is used by JavaScript to find the element.
Summary on how the browser loads a page containing JavaScript
Let’s revisit how the interplay between browser and server unfolds when the page https://fullstack-exampleapp.herokuapp.com/notes is opened.
- Fetch the HTML code defining the content and basic structure of the page using a HTTP GET request
- Links in the HTML cause other stuff to be fetched, in this case:
- the stylesheet
main.css
and - the JavaScript file
main.js
- the stylesheet
- Now, the JavaScript code is executed. Another HTTP GET request is made to fetch the JSON Data from https://fullstack-exampleapp.herokuapp.com/data.json
- When the data is fetched, the browser executes the event handler, which renders the notes to the page using the DOM-API.
Forms and HTTP POST
Using the form on the notes page to add a new note causes five HTTP requests:
- The first is an HTTP POST request to the server address ending in
new_note
to which the server replies with HTTP status code 302. This is a URL redirect, with which the server is asked to do a - new HTTP GET request to the address defined in the header’s Location, i.e.
the address
notes
: - Fetching
main.css
- Fetching
main.js
- Fetching notes
data.json
In the HTML code the, form
tag has attributes action
and method
, which
stipulate that submitting the form is done as an HTTP POST request to the
address new_note
.
On the server, we now need some simple code to handle the POST request. Remember: This is code running on the server and not on the browser.
The server does not save the array containing content
and date
to a
database, so new notes vanish when Heroku restarts the service
AJAX
The notes page from above uses a concept called Asynchronous JavaScript and XML (AJAX) which became popular in the early 2000s. It describes a revolutionary approach based on the advancements in browser technology that enabled the fetching of content using JavaScript included within the HTML without the need to rerender the entire page. On Wikipedia, it says that
With Ajax, web applications can send and retrieve data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page. By decoupling the data interchange layer from the presentation layer, Ajax allows web pages and, by extension, web applications, to change content dynamically without the need to reload the entire page.
Prior to AJAX, everything that one could see in the browser was rendered HTML-code generated by the server that had to be rerendered once something changed on the server.
While the notes page uses AJAX to display the notes, the application URLs
(.../data.json
, .../new_note
) reflect bad practice nowadays as they don’t
follow the coventions of RESTful APIs, which will be
covered later.
Nowadays, everything uses AJAX so the term has become somewhat meaningless.
Single Page Applications (SPA)
While “traditional” web applications have all the logic running on the server with the browser continually rerendering the HTML it fetches, SPAs consist of a single HTML page, the contents of which are manipulated with JavaScript that executes in the browser.
The notes page is almost there. However, as we saw above, adding new notes still depends on the server dealing with a POST request and instructing the browser to reload the page with a 302 redirect.
In the
SPA version of the notes page,
we give the form an id
tag such that we can write JavaScript that handles the
note creation process. Now, upon filling out the form, the browser sends just
one request with the content-type application/json
:
Transclude of 2020-06-06_09-26-28_screenshot.png"
Let’s take a look at the part of JavaScript code in spa.js
dealing with the
form submission:
Nonetheless, even the SPA version of the page does not adhere to current best practices.
JavaScript Libraries
The application above is mainly coded in pure or “vanilla” JavaScript as it only uses the DOM-API and built-in JavaScript features to manipulate the structure of the page. There is a range of libraries containing tools that make it easier to interact with the DOM-API. Some of the most popular include:
- jQuery was used mainly back in the day for its cross-browser support but fell out of favour once VanillaJS and browsers can do most/all of the stuff it offered.
- After BackboneJS, AngularJS became the de-facto standard of modern webdevelopment after its initial release by Google in 2012. Since Angular 2 was not designed to be backwards compatible with prior versions, it became less popular.
- Today, the most popular tool for implementing the browser-side logic of web-applications is Facebook’s React library
Exercises 0.1.-0.6.
0.1 HTML
Review the basics of HTML by reading this tutorial from Mozilla.
0.2: CSS
Review the basics of CSS by reading this tutorial from Mozilla.
0.3: HTML forms
Learn about the basics of HTML forms by reading Mozilla’s tutorial “Your first form”.
0.4: new note
In chapter Loading a page containing JavaScript - revised the chain of events caused by opening the page https://fullstack-exampleapp.herokuapp.com/notes is depicted as a sequence diagram
The diagram was made using websequencediagrams service as follows:
Create a similar diagram depicting the situation where the user creates a new note on page https://fullstack-exampleapp.herokuapp.com/notes by writing something into the text field and clicking the submit button.
If necessary, show operations on the browser or on the server as comments on the diagram.
The diagram does not have to be a sequence diagram. Any sensible way of presenting the events is fine.
All necessary information for doing this, and the next two exercises, can be found from the text of this part. The idea of these exercises is to read the text through once more, and to think through what is going on where. Reading the application code is not necessary, but it is of course possible.
0.5: Single Page App
Create a diagram depicting the situation where the user goes to the single page app version of the notes app at https://fullstack-exampleapp.herokuapp.com/spa.
0.6: New Note
Create a diagram depicting the situation where user creates a new note using the single page version of the app.
This was the last exercise, and it’s time to push your answers to GitHub and mark the exercises as done in the submission application.
My solutions to the exercises are published here.
Intro to React
Debugging React Apps
Rules of Hooks
Never use useState
or useEffect
inside a loop. Only ever call hook from
inside a function body defining a React component:
Event Handlers
Assume we develop this application:
The clicking of a button should reset the state stored in the value
variable.
Thus, we need an even handler. Now, this is important: An event handler must
always be a function or a reference to a function. Nothing else works. Don’t
use a function call, i.e. setValue(0)
either (there is one exception, see
below). Only functions and references to functions such as:
or:
You can also define an event handler to use a function that returns another function. Function inception. Then you can actually use a function call in the event handler because the returned object is a function. You can use this functionality if you need generic functionality (e.g. greetings via the console) with parameters (e.g. user names):
A more compact way to write the hello
function would be:
We can also pass event handlers to child components. So let’s extract the button into its own component:
Another important rule is to never define components within components. It’s nasty.
Communicating with Server
Rendering a collection, modules
What’s the difference between an experienced JavaScript programmer and a rookie? The experienced one uses
console.log
10-100 times more.
JavaScript const
, let
and var
- was slightly confused and then read
this article
which cleared up my understanding. It boils down to the fact that
var
should be avoided. - Both
let
andconst
are block-scoped and hoisted to the top. While variables declared withlet
can be updated but not redeclared,const
-declared variables can be neither. However, you can update the property of aconst
object as so:
Functional Programming in JavaScript
- Watch this
video series.
It’s pretty good. Also watch
this for a great explanation
of
map
andreduce
. - In every functional programming language, functions are values and you can
exploit this by dividing your code into small and simple functions that can be
composed together using higher order functions such
filter
,reject
,map
orreduce
. See the following example for filter:
map
does not throw objects out of an array likefilter
orreject
based on a Boolean value. Instead it transforms them:
reduce
is the swiss-army knife of list transformations. You can always fall back on it when the other built-in higher-order functions don’t solve your problem.
Anti-pattern: array indexes as keys
Avoid using the index of an array within the map function in React. See here for more infos. Possibly use shortid for ID-creation.
Do it like this instead:
Forms
- See the example projects (esp.
phonebook
)
Getting data from server
- Watch this video about event loops in JavaScript.
- See the example projects in github and their final solutions (esp.
countries
)
Altering data in server
- Routes are URLs + HTTP request types
- Never mutate state directly. If a state is an array use a method like
concat
to create a new array (seenotes
app) - You need to understand promises. They are an integral part of modern JavaScript. The third Chapter of You Don’t Know JS and this article should be read and understood.
Adding styles to React app
Programming a Server with NodeJS and Express
RESTful APIs
- see this
article
for a definition of the different levels of RESTful maturity. In this course,
we are mostly operating at level 2.
- Resources
- HTTP verbs (
GET
,POST
,PUT
DELETE
,PATCH
) - Multimedia
Difference between SQL and NoSQL
{{< youtube ruz-vK8IesE >}}
Testing Express Servers, User Administration
Testing React Apps
State Management with Redux
React Router, Custom Hooks, Styling App with CSS and Webpack
GraphQL
Typescript
Resources
<~/Exocortex/bib/library.bib>