-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy patho.html
More file actions
387 lines (343 loc) · 14.5 KB
/
Copy patho.html
File metadata and controls
387 lines (343 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!--Section: Docs content-->
<section>
<section id="intro">
<!-- Main title -->
<h2 class="h1 fw-bold"><?= $data_var['main_title']; ?></h2>
<!-- Seo title -->
<h1 class="h6"><?= $data_var['seo_title']; ?></h1>
<!-- Description -->
<p>
After reading this tutorial you will know how to create full-stack apps using MEVN stack.
</p>
</section>
<hr class="my-5" />
<section id="whatis-mevn">
<!-- Section title -->
<h2 class="mb-4">What does MEVN stand for?</h2>
<div class="row">
<div class="col-md-12">
<p>
<strong>MEVN</strong> is an acronym for web development tech stack consisting of these
four technologies:
</p>
<ul>
<li><strong>M</strong>ongoDB</li>
<li><strong>E</strong>xpress.js</li>
<li><strong>V</strong>ue</li>
<li><strong>N</strong>ode.js</li>
</ul>
<p>Let's quickly recall what are these.</p>
</div>
</div>
</section>
<section id="mevn-recap">
<!-- Section title -->
<h2 class="mb-4">MEVN recap</h2>
<div class="row">
<div class="col-md-12">
<p>Here's a quick recap what are the main technologies used in the MEVN stack:</p>
<ul>
<li>
<strong>MongoDB</strong> is a document-oriented NoSQL database mevnt to store
non-tabular data. If you want to learn more about it check out a
<a href="/docs/standard/cli/getting-started/mongo/" target="_blank">separate tutorial</a
>.
</li>
<li>
<strong>Express.js</strong> is a web framework built for Node.js that allows you to
create web servers in a very convenient way. Learn more about it
<a href="https://mdbootstrap.com/docs/standard/cli/getting-started/express/">here</a>.
</li>
<li>
<strong>Vue</strong> is a JavaScript framework that allows you to create very advanced
single-page apps. You can check out how to create an Vue project in
<a href="/docs/standard/cli/getting-started/vue/">another tutorial</a>.
</li>
<li>
<strong>Node.js</strong> is a multi-platform JavaScript runtime built on V8 engine. It's
used as a backend side of web apps. Read more about it
<a href="https://mdbgo.com/docs/tutorials/node/">here</a>.
</li>
</ul>
<p>
We won't dive deep into details about how to get started with these technologies using MDB
GO. Go ahead and check the linked tutorials and once you learn a few things get back here.
</p>
<p>If you have enough knowledge to get started, let's do this.</p>
</div>
</div>
</section>
<section id="building-mevn-app">
<!-- Section title -->
<h2 class="mb-4">Building the MEVN app</h2>
<div class="row">
<div class="col-md-12">
<p>
Our example app will be a TODO list. It's a very popular use-case so let's simply get
things done one at a time. We will go through the following steps:
</p>
<ul>
<li>Create a MongoDB database named <code>todo_app</code></li>
<li>Create Node.js API with Express and Mongoose</li>
<li>Connect our frontend app to the Node.js API.</li>
</ul>
<p>Let's go!</p>
<h3>Creating MongoDB database</h3>
<p>In order to create a new database you need to run the following command:</p>
<mdbsnippet>
<code data-lang="shell" data-name="Shell">
$ mdb database init -db mongodb
</code>
</mdbsnippet>
<p>
Now you need to provide your user data and then the database details. Please provide the
values of your choice. For example:
</p>
<pre><code>? Enter username tony
? Enter password Stark_12
? Repeat password Stark_12
? Enter database name todo_app
? Enter description Database for the TODO app
</code></pre>
<p class="note note-warning">
<strong>Note:</strong> the password must contain <i>at least</i> one uppercase letter, one
lowercase letter, one number, one special symbol and have minimum length of 8.
</p>
<p>Hit <kbd>Enter</kbd> and it's done.</p>
<p>
Please notice that in the command output the username and database name slightly differs
from what you provided earlier. Don't worry - it's OK. A few random characters have been
added to these values to randomize them and improve security of your database credentials.
</p>
<p class="note note-danger">
<strong>Important:</strong> Do not close your terminal window until you save your
credentials somewhere. This is the only time we will show you your database password. If
you won't save it you'll loose it.
</p>
<p>
That's pretty much it. You can now see how to connect with this database from the Node.js
API.
</p>
<p class="note note-warning">
<strong>Note:</strong> in MongoDB you don't need to create collections before they are
used. MongoDB will create a collection when it's first time accessed.
</p>
<h3>Creating Node.js API with Express and Mongoose</h3>
<p>
In case you don't know, Mongoose is a MongoDB object modeling tool. It helps managing
MongoDB access and models. You can initialize a MDB GO starter that already has configured
Mongoose. Simply run the following command:
</p>
<mdbsnippet>
<code data-lang="shell" data-name="Shell">
$ mdb backend init
</code>
</mdbsnippet>
<p>and choose <code>Express.js + MongoDB API starter</code> from the list that shows up.</p>
<p>
After initialization just go to the newly created project directory and open the
<code>.env</code> file. After that edit the <code>DB_CONNECTION_STRING</code> value. You
should paste the connection string that's been shown to you in the previous step. In my
case the updated value looks like this:
</p>
<pre><code>DB_CONNECTION_STRING=mongodb://tonye9c88223:Stark_12@mongo.db.mdbgo.com:8604/todo_appbd2f70e0</code></pre>
<p>Save the file and close it.</p>
<p>
The starter project is ready to use. You don't have to change anything to run the example
app for the sake of this tutorial. Simply publish it using the following command:
</p>
<mdbsnippet>
<code data-lang="shell" data-name="Shell">
$ mdb publish -p node12
</code>
</mdbsnippet>
<p>
After your files get uploaded you can access your app under the address shown in the
output.
</p>
<p class="note note-danger">
<strong>Note:</strong> You may want to check
<a href="https://mdbgo.com/docs/tutorials/node/">the tutorial</a> where we explain in
detail what can go wrong here and how to deal with it.
</p>
<p class="note note-warning">
<strong>Note:</strong> Since we need to install dependencies and run your app, it may take
a few moments until it will be available under the provided URL.
</p>
<p>
When you open the app URL you will see the default view on the <code>/</code> endpoint but
when you'll visit <code>/tasks</code> you will see an empty array indicating that the
<code>tasks</code> collection has been created and is empty. There are also other
endpoints defined like this:
</p>
<ul>
<li><code>GET /tasks/:id</code></li>
<li><code>POST /tasks</code></li>
<li><code>PUT /tasks/:id</code></li>
<li><code>DELETE /tasks/:id</code></li>
</ul>
<p>
These are self-explanatory but if you don't know how to operate on that go and check the
<a href="https://mdbgo.com/docs/tutorials/node/">tutorial</a> where it is explained.
</p>
<p>
Alright, only one step left for the fully-working TODO app - Vue frontend. Let's create
it.
</p>
<h3>Connecting to the API via Vue app</h3>
<p>
We won't show how to create the whole Vue app here. Instead, you'll learn how to connect
to the API <strong>assuming you already have a working app</strong>.
</p>
<p>
It's actually pretty easy. All we need to use if JavaScript Fetch API. Let's jump right
into it.
</p>
<p>
First, let's quickly have a look where we are going to place our code. We are assuming you
have the <code>App.vue</code> file where you want to put API communication logic. It
should look somewhat like this:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
export default {
name: "App",
data() {
return {
tasks: [],
};
},
mounted() {
// ...
},
};
</code>
</mdbsnippet>
<p>
It's also wise to add <code>apiUrl</code> constant in our component so that we don't have
to repeat it every time:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
const apiUrl = 'http://express-mongoose-starter.mdbgo.io'; // in your case it will be different
</code>
</mdbsnippet>
<p>
We will be adding our methods in the <code>methods</code> field. Add it right after the
<code>mounted()</code> method:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
mounted() {
// ...
},
methods: {
}
</code>
</mdbsnippet>
<p>
In order to read data from the server we need to make a <code>GET /tasks</code> request.
You can do it like this:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
methods: {
getTasks() {
return fetch(`${this.apiUrl}/tasks`)
.then(res => res.json());
}
}
</code>
</mdbsnippet>
<p>
It's a new method that performs the GET request and returns the result. We will take care
of this result later. In a meantime let's add the remaining three methods:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
methods: {
// ...
createTask(name, desc) {
const data = { name, desc };
return fetch(`${this.apiUrl}/tasks`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data);
})
.then((res) => res.json());
}
updateTask(id, name, desc) {
const data = { name, desc };
return fetch(`${this.apiUrl}/tasks/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data);
})
.then((res) => res.json());
}
deleteTask(id) {
return fetch(`${this.apiUrl}/tasks/${id}`, { method: 'DELETE' })
.then((res) => res.json());
}
}
</code>
</mdbsnippet>
<p>Alright, let's find out how to use this.</p>
<h4>Calling the API from the Vue component</h4>
<p>
In order to use our methods we can call them in the <code>mounted()</code> hook for
example like this:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
mounted() {
this.getTasks()
.then((tasks) => {
this.tasks = tasks
});
}
</code>
</mdbsnippet>
<p>It will make an API request and assign the result to the <code>tasks</code> field.</p>
<p>Making POST, PUT and DELETE requests is just as easy:</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="javascript" data-name="App.vue">
mounted() {
// Create a task
this.createTask('Wash the dishes', 'will take a while')
.then((task) => {
this.tasks.push(newTask);
});
// Update a task
const currentTaskId = 1;
this.updateTask(currentTaskId, 'Wash the dishes', 'will take 30 mins')
.then((updatedTask) => {
const oldTask = this.tasks.find((t => t.id === currentTaskId));
oldTask.id = updatedTask.id;
oldTask.name = updatedTask.name;
oldTask.desc = updatedTask.desc;
});
// Delete a task
const currentTaskId = 1;
this.deleteTask(currentTaskId)
.then(() => {
const idx = this.tasks.findIndex(t => t.id === currentTaskId);
this.tasks.splice(idx, 1);
});
}
</code>
</mdbsnippet>
<p>And that's how you communicate with your API from the Vue app.</p>
</div>
</div>
</section>
<!--Section: Docs content-->
</section>
<!--Section: Docs content-->