You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: lessons/03-persistence/03-persistence.md
+22-28
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,14 @@ Sam Tarakajian for NYU
8
8
9
9
## Essential Questions
10
10
- How can we persist data over a long period of time, even if the server restarts?
11
-
- What is CRUD? How can we implement it?
11
+
- What is BaaS?
12
+
- What is CRUD?
12
13
13
14
## Introduction
14
15
We've seen how to put a Node server on the Internet. The question is, how does that server store data? It could store data in memory, but then that goes away when the server closes. It could write files to disk, but file access is slow, especially if multiple people are trying to read/write a file at the same time.
The first three Node classes are prerequisite for this class.
18
+
The first three classes are prerequisite for this class.
18
19
19
20
### Outcomes & Goals
20
21
* In this workshop we will be looking at how to implement data persistance on a web server. We will store data on a server, and update that data using an API call.
@@ -34,23 +35,15 @@ This workshop is indended to last about three hours
34
35
- nvm
35
36
- git
36
37
- Postman (https://www.postman.com/)
37
-
- mongodb
38
-
- See https://docs.mongodb.com/manual/administration/install-community/ for platform-specific installation instructions
39
-
- sign up for heroku
40
-
- (recommended) mongodb compass https://www.mongodb.com/download-center/compass which you can use to visualze your database
res.send(`This page has been viewed ${data.pageviews} times`);
134
-
135
-
} catch (e) {
136
-
res.send("Some kind of terrible error happened");
137
127
138
-
console.dir(e);
128
+
} catch (e) {
129
+
next(e);
139
130
}
140
131
});
141
132
@@ -151,7 +142,7 @@ This is better (and also gives us a chance to talk about Promises and async/awai
151
142
152
143
Now we get to talk about something fun called a database.
153
144
154
-
### Mongodb
145
+
### Databases
155
146
156
147
A database does a few important things for us:
157
148
@@ -160,17 +151,20 @@ A database does a few important things for us:
160
151
- It provides an API, which means the database can be hosted in a number of ways (important for working with a VM).
161
152
- It takes care of a lot of messy details for us, like concurrency (two people touching the database at the same time) and consistency (not a problem for us, but a problem for some people).
162
153
163
-
Let's start a database! See the instructions on `https://docs.mongodb.com/manual/administration/install-community/`
164
-
165
154
Interacting with the database is a lot like interacting with a remote API. We don't actually deal with modifying any data files. Rather, we connect to the database server, and we handle requests by interacting with that server on the behalf of our client.
166
155
167
156
`Client -> Server (Our express server) -> Database Server (We don't write this) -> Actual database files`
168
157
169
-
See [this file](./src/mongo-basic/app.js) for the basics. As you can see, this app is able to stop and restart, while still keeping all of its data.
158
+
### Starting with Back4App
159
+
160
+
Back4App has a silly name, but it gives you a really streamlined way to interact with a relational database. To understand what we're doing, it might help to picture how all the different parts of our web application fit together.
161
+
162
+

170
163
164
+
Our server is fulfilling two roles simultaneously. First, it's serving the entire website, aka the client application. This is the page that actually loads when a person visits your website. Second, it's providing an API—and Application Programming Interface—that the website can use to fetch data from your backend database. Dynamic websites like Twitter and Facebook work the same way. When you go to twitter.com, first the webpage itself loads, and then a separate request gets the content that should actually be displayed in the page. The page doesn't connect to the database directly, but uses an interface that interacts with the database on its behalf. It's a bit like going to the bank; when you ask to make a withdrawal, the bank doesn't open its vault and trust you to take the right amount. Instead, there's an interface that does all of that on your behalf
171
165
### Mongodb plus Heroku
172
166
173
-
Let's throw this up on Heroku. So right now we're running Mongodb locally. We could maybe figure out a way to tell Heroku to install Mongo on our virtual machine, but a better way is with something called a Heroku add-on. Log into Heroku, click on the app you want to use (we could use the same app we used last week, or create a new free one, whichever you prefer). Next, add the mLab add-on. This adds a Mongodb server that will start in parallel to our Heroku VM.
167
+
Let's throw this up on Heroku. So right now we're running Mongodb locally. We could maybe figure out a way to tell Heroku to install Mongo on our virtual machine, but a better way is with something called a Heroku add-on. Log into Heroku, click on the app you want to use (we could use the same app we used last week, or create a new free one, whichever you prefer). Next, add the mLab add-on. This adds a Mongodb server that will start in parallel to our Heroku VM.
174
168
175
169
If you want to push your new repo up to Heroku, follow the usual steps:
***With thanks and acknowledgement, this is based on the template provided by [Eyebeam](https://github.com/eyebeam/curriculum/blob/master/TEMPLATE.md)***
539
+
***With thanks and acknowledgement, this is based on the template provided by [Eyebeam](https://github.com/eyebeam/curriculum/blob/master/TEMPLATE.md)***
0 commit comments