GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add changes :ant: · jokoss92/fullstack-javascript@2ce0445

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • chapter3
    • chapter3.md
  • chapter4
    • chapter4.md
  • chapter5
    • chapter5.md
  • chapter8
    • chapter8.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you'd like to contribute financially towards the effort (or any of my other o
2323
* [Chapter 5: Backbone.js and Parse.com](https://github.com/azat-co/fullstack-javascript/tree/master/chapter5/chapter5.md)
2424
* [Chapter 6: Intro to Node.js](https://github.com/azat-co/fullstack-javascript/tree/master/chapter6/chapter6.md)
2525
* [Chapter 7: Intro to MongoDB](https://github.com/azat-co/fullstack-javascript/tree/master/chapter7/chapter7.md)
26-
* [Chapter 8: Putting It All Together](https://github.com/azat-co/fullstack-javascript/tree/master/chapter8/chapter8.md)
26+
* [Chapter 8: Putting Frontend and Backend Together](https://github.com/azat-co/fullstack-javascript/tree/master/chapter8/chapter8.md)
2727
* Conclusion and Further Reading
2828

2929
(It's work in progress and the draft will be completed in the end of 2017.)
@@ -170,7 +170,7 @@ TBD
170170
* [07-db-connect-heroku](https://github.com/azat-co/fullstack-javascript/tree/master/code/07-db-connect-heroku): Heroku database connection test with MongoDB Native Driver
171171
* [07-db-server](https://github.com/azat-co/fullstack-javascript/tree/master/code/07-db-server): Server to list MongoDB collections
172172
* [07-board-api-mongo](https://github.com/azat-co/fullstack-javascript/tree/master/code/07-board-api-mongo): Message Board API with MongoDB
173-
* [08-board-ui](https://github.com/azat-co/fullstack-javascript/tree/master/code/08-board-ui): Message Board UI (front-end) without back end
173+
* [08-board-ui](https://github.com/azat-co/fullstack-javascript/tree/master/code/08-board-ui): Message Board UI (front-end) without backend
174174
* [08-board-api](https://github.com/azat-co/fullstack-javascript/tree/master/code/08-board-api): Message Board back-end with CORS
175175
* [board-react](https://github.com/azat-co/fullstack-javascript/tree/master/code/board-react): Message Board with React.js view layer - BONUS PROJECT
176176

Collapse file

‎chapter3/chapter3.md‎

Copy file name to clipboardExpand all lines: chapter3/chapter3.md
+3-6Lines changed: 3 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This chapter is a basic introduction to front-end web development. It covers thi
2525

2626
This chapter also covers the terminology and explains JSON, AJAX, and CORS. We then explore the example of a weather app.
2727

28-
We use Parse as our back end to streamline things and make development faster while still keeping it realistic. The cornerstone of this chapter is a persistent message board application built with Parse and jQuery.
28+
We use Parse as our backend to streamline things and make development faster while still keeping it realistic. The cornerstone of this chapter is a persistent message board application built with Parse and jQuery.
2929

3030
Definitions
3131
===========
@@ -991,10 +991,7 @@ Let’s move on to the Message Board app.
991991
Message Board with Parse Overview
992992
=====================================
993993
994-
The Message Board will consist of an input field, a list of messages,
995-
and a "SEND" button (see Figure 3-5). We need to display a list of existing messages and be
996-
able to submit new messages. We'll use Parse as a back end for now,
997-
and later switch to Node.js with MongoDB.
994+
The Message Board will consist of an input field, a list of messages, and a "SEND" button (see Figure 3-5). We need to display a list of existing messages and be able to submit new messages. We'll use Parse as a backend for now, and later switch to Node.js with MongoDB.
998995
999996
![](media/board-1.png)
1000997
@@ -1030,7 +1027,7 @@ origin domain AJAX calls, so we won't need JSONP.
10301027
10311028
When you decide to deploy your back-end application, which will act as a
10321029
substitute for Parse, on a different domain you'll need to use
1033-
either JSONP on the front end or custom CORS headers on a back end. This
1030+
either JSONP on the front end or custom CORS headers on a backend. This
10341031
topic is covered later in the book.
10351032
10361033
Right now the structure of the application should look like this:
Collapse file

‎chapter4/chapter4.md‎

Copy file name to clipboardExpand all lines: chapter4/chapter4.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ endpoint substitute, which provides us with names and image URLs of the
234234
apples (data models).
235235

236236
Note that this mock data set can be easily substituted by assigning REST
237-
API endpoints of your back end to `url` properties in Backbone.js
237+
API endpoints of your backend to `url` properties in Backbone.js
238238
collections, models, or both, and calling the `fetch()` method on them.
239239

240240
Now to make the user experience a little bit better, we can add a new
Collapse file

‎chapter5/chapter5.md‎

Copy file name to clipboardExpand all lines: chapter5/chapter5.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ Summary
658658
This short chapter gives you yet another way of building apps with
659659
nothing but JavaScript (and HTML and CSS, obviously). With Parse or
660660
a similar back-end-as-a-service (BaaS) solution, it is straightforward
661-
to persist the data without having to code your own back end. BaaS
661+
to persist the data without having to code your own backend. BaaS
662662
solutions take it a step further by allowing for access-level
663663
controls, authentications, server-side logic, and third-party
664664
integrations.
Collapse file

‎chapter8/chapter8.md‎

Copy file name to clipboardExpand all lines: chapter8/chapter8.md
+30-27Lines changed: 30 additions & 27 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
CHAPTER 8
44
---------
55

6-
Putting It All Together
6+
Putting Frontend and Backend Together
77
=======================
88

99
> *Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.*
@@ -12,19 +12,21 @@ Putting It All Together
1212
1313
In this chapter, we'll cover:
1414

15-
- Adding CORS for Different Domain Deployment
15+
- Adding CORS for a different-domain deployment
1616
- Message Board UI
1717
- Message Board API
1818
- Deployment to Heroku
19-
- Same Domain Deployment Server
19+
- Same-domain deployment server
2020
- Deployment to Amazon Web Services
2121

22-
Now, it would be good if we could put our front-end and back-end applications so they could work together. There are a few ways to do it:
22+
Now, it's a good time to configure our front-end and back-end applications so they could work together. There are a few ways to do it:
2323

24-
- Different domains (Heroku apps) for front-end and back-end apps: make sure there are no cross-domain issues by using CORS or JSONP. This approach is covered in detail later.
25-
- Same domain deployment: make sure Node.js process static resources and assets for front-end application—not recommended for serious production applications.
24+
- Different-domain deployment: deploy two separate apps (Heroku apps) for front-end and back-end apps. Developers need to be implement CORS or JSONP to mitigate cross-domain request issues.
25+
- Same-domain deployment: deploy frontend and backend on the same webapp. Developers need to implement a static Node.js server to serve static assets for the front-end application. This approach is not recommended for serious production applications.
2626

27-
Adding CORS for Different Domain Deployment
27+
I cover both approaches in this in detail in this chapter starting with the recommended approach —different-domain deployment.
28+
29+
Adding CORS for Different-Domain Deployment
2830
===========================================
2931

3032
This is, so far, the best practice for the production environment. Back-end applications are usually deployed at the `http://app.` or `http://api.` subdomains.
@@ -41,30 +43,30 @@ const request = $.ajax({
4143
})
4244
```
4345
44-
The other, and better, way to do it is to add the OPTIONS method, and special headers, which are called [cross-origin resource sharing](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) or CORS (<https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>), to the Node.js server app before the output:
46+
The other, and better, way to do it is to add the cross-origin resource sharing or CORS support on the server (<https://mzl.la/2LcH9lZ>). It will require to add the OPTIONS method request handler and special headers to other request handlers to the Node.js server app before the output/response. This is a special header that needs to be added to all request handlers. The `origin` is the domain but can be open to anything with `*`:
4547
4648
```js
4749
...
4850
response.writeHead(200,{
4951
'Access-Control-Allow-Origin': origin,
50-
'Content-Type':'text/plain',
51-
'Content-Length':body.length
52+
'Content-Type': 'text/plain',
53+
'Content-Length': body.length
5254
})
5355
...
5456
```
5557
56-
or
58+
or `origin` value can be locked to only your front-end app location (recommended):
5759
5860
```js
5961
...
6062
res.writeHead(200, {
61-
'Access-Control-Allow-Origin', 'your-domain-name',
63+
'Access-Control-Allow-Origin', 'your-fe-app-domain-name',
6264
...
6365
})
6466
...
6567
```
6668
67-
The need for the OPTIONS method is outlined in [HTTP access control (https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)](https://developer.mozilla.org/en-US/docs/HTTP_access_control). The OPTIONS request can be dealt with in the following manner:
69+
We need a new response (route or request handler) with the OPTIONS method to tell the client (browser) what methods are supported on the server. The OPTIONS request can be implemented in the following manner:
6870
6971
```js
7072
...
@@ -74,7 +76,7 @@ The need for the OPTIONS method is outlined in [HTTP access control (https://dev
7476
"Access-Control-Allow-Methods":
7577
"GET, POST, PUT, DELETE, OPTIONS",
7678
"Access-Control-Allow-Headers": "content-type, accept",
77-
"Access-Control-Max-Age": 10, // Seconds.
79+
"Access-Control-Max-Age": 10, // In seconds
7880
"Content-Length": 0
7981
})
8082
response.end();
@@ -85,16 +87,16 @@ The need for the OPTIONS method is outlined in [HTTP access control (https://dev
8587
Message Board UI
8688
================
8789
88-
Our front-end application used Parse.com as a replacement for a back-end application. Now we can switch to our own back end replacing the endpoints along with a few other painless changes. Let me walk you through them.
90+
Our front-end application used Parse.com as a replacement for a back-end application. Now we can switch to our own backend by replacing the endpoints along with making a few other painless changes. Let me walk you through them.
8991
90-
In the beginning of the `app.js` file, uncomment the first line for running locally, or replace the URL values with your Heroku or Windows Azure back-end application public URLs:
92+
In the beginning of the `app.js` file, uncomment the first line for running locally, or replace the URL values with your Heroku or Microsoft Azure back-end application public URLs:
9193
9294
```js
9395
// const URL = 'http://localhost:1337/'
9496
const URL ='http://your-app-name.herokuapp.com/'
9597
```
9698
97-
Most of the code in `app.js` and the folder structure remained intact from the `06-board-backbone-parse-sdk` project, with the exception of replacing Parse.com models and collections with original Backbone.js ones. So go ahead and type or copy the RequireJS block for loading of the dependencies (templates in this case):
99+
Most of the code in `app.js` and the folder structure remained intact from the `code/05-board-backbone-parse-sdk` project (<http://bit.ly/2LfB9IQ>), with the exception of replacing Parse models and collections with original Backbone.js ones. So go ahead and type or copy the RequireJS block for loading of the dependencies (templates in this case):
98100
99101
```js
100102
require([
@@ -107,9 +109,9 @@ require([
107109
footerTpl) {
108110
```
109111
110-
The `ApplicationRouter`, `HeaderView``,` and `FooterView` are the same as in the `06-board-backbone-parse-sdk` project so I won't list them here again.
112+
The `ApplicationRouter`, `HeaderView,` and `FooterView` are the same as in the `code/05-board-backbone-parse-sdk` project so I won't list them here again.
111113
112-
We need to change the the model and collection to this from using `Parse.Object` and `Parse.Collection`. Those are the places where Backbone.js looks up for REST API URLs corresponding to the specific collection and model:
114+
We need to change the model and collection to this from using `Parse.Object` and `Parse.Collection`. Those are the places where Backbone.js looks up for REST API URLs corresponding to the specific collection and model:
113115
114116
```js
115117
Message = Backbone.Model.extend({
@@ -132,22 +134,24 @@ Next is the `HomeView` where most of the logic resides. I made a few enhancement
132134
},
133135
```
134136
135-
Now, in the constructor of the view set the `homeView` to `this` so we can use `this` later by the name inside of the closures (otherwise, `this` can mutate inside of the closures):
137+
Now, in the constructor of the view, set the `homeView` to `this` so we can use `this` later by the name inside of the callbacks/closures or use fat arrow functions `()=>{}` (otherwise, `this` can mutate inside of the callbacks/closures):
136138
137139
```js
138140
initialize: function() {
139141
const homeView = this
140142
```
141143
142-
Then, I attached an event listener `refresh` that will do the rendering. Prior to that we had the `all` event, which wasn't very good, because it triggered re-rendering the addition of each message. You see, `fetch` will trigger `add` as many times as there are messages (10, 100, 1000, etc.) and if we use `all` event listener, `add` is part of `all`. While with this custom event `refresh` we can trigger rendering in the appropriate places (you'll see them later).
144+
Next, I attach an event listener `refresh` that will do the rendering. Prior to that we had the `all` event, which wasn't very good, because it triggered re-rendering multiple times on the addition of each message. The reason is that `fetch()` triggers `add()` as many times as there are messages (10, 100, 1000, etc.). So if we use the `all` event listener for `render()`, our app will unnecessarily render multiple times. A better way is to use a custom event `refresh` that we will trigger manually and only in the appropriate places (you'll see them later). This will prevent multiple re-rendering.
145+
146+
The following code creates the collection, creates the `refresh` event and starts the `fetch` request to populate the messages from the backend:
143147
144148
```js
145149
homeView.collection = new MessageBoard()
146150
homeView.collection.bind('refresh', homeView.render, homeView)
147151
homeView.collection.fetch({
148152
```
149153
150-
The `fetch` method will perform GET XHR request and it has `success` and `error` callbacks (indentation removed):
154+
The `fetch` method will perform a GET XHR request, and it has `success` and `error` callbacks (indentation removed):
151155
152156
```js
153157
success: function(collection, response, options){
@@ -165,7 +169,7 @@ The next line will trigger rendering only after all the messages are in the coll
165169
})
166170
```
167171
168-
This event listener will be triggered by the SEND button as well as by the `fetch`. To avoid persisting existing records with `message.save()`, we add the check for the `message.attributes._id`. In other words, if this an existing message and it comes from the server (`fetch`), then it will have `_id` and we stop the execution flow. Otherwise, we persist the message and trigger rendering on success:
172+
This event listener will be triggered by the "SEND" button as well as by the `fetch`(). To avoid persisting existing records with `message.save()`, we add the check for the `message.attributes._id`. In other words, if this is an existing message and it comes from the server (`fetch`), then it will have `_id` and we stop the execution flow. Otherwise, we persist the message and trigger rendering on success:
169173
170174
```js
171175
homeView.collection.on('add', function(message) {
@@ -183,7 +187,7 @@ This event listener will be triggered by the SEND button as well as by the `fetc
183187
},
184188
```
185189
186-
The rest of the `HomeView` object is the same as in the `06-board-parse-sdk` project. In the `saveMessage` we get the values of the username and the message text and add the new message object to the collection with `collection.add()`. This will call the event listener `add`, which we implemented in the `initialize`.
190+
The rest of the `HomeView` object is the same as in the `05-board-parse-sdk` project. In the `saveMessage` we get the values of the username and the message text and add the new message object to the collection with `collection.add()`. This will call the event listener `add`, which we implemented in the `initialize`.
187191
188192
```js
189193
saveMessage: function(){
@@ -211,8 +215,7 @@ Last, we write or copy the `render` method that takes the template and the colle
211215
})
212216
```
213217
214-
Here is the full source code of the [08-board-ui/app.js](https://github.com/azat-co/fullstack-javascript/blob/master/code/08-board-ui/app.js)
215-
file (https://github.com/azat-co/fullstack-javascript/blob/master/code/08-board-ui/app.js):
218+
Here is the full source code of the `code/08-board-ui/app.js` file (<http://bit.ly/2LaHhCp>):
216219
217220
```js
218221
const URL = 'http://localhost:1337/'
@@ -313,7 +316,7 @@ require([
313316
})
314317
```
315318
316-
This is it. For your reference, the front-end app source code is at [https://github.com/azat-co/fullstack-javascript/tree/master/code/08-board-u](https://github.com/azat-co/fullstack-javascript/tree/master/code/08-board-ui) in the GitHub folder. I won't list it here because we had only a few changes comparing with the Parse SDK project. The next piece of the puzzle is the back end.
319+
This is it. For your reference, the front-end app source code is in the `code/08-board-ui` folder and on GitHub at <http://bit.ly/2LfD1Bo>. I won't list it here because we had only a few changes comparing with the Parse SDK project. The next piece of the puzzle is the backend.
317320
318321
Message Board API
319322
=================

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.