-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy patho.html
More file actions
449 lines (396 loc) · 16.7 KB
/
Copy patho.html
File metadata and controls
449 lines (396 loc) · 16.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!--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 MEAN stack.
</p>
</section>
<hr class="my-5" />
<section id="whatis-mean">
<!-- Section title -->
<h2 class="mb-4">What does MEAN stand for?</h2>
<div class="row">
<div class="col-md-12">
<p>
<strong>MEAN</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>A</strong>ngular</li>
<li><strong>N</strong>ode.js</li>
</ul>
<p>Let's quickly recall what are these.</p>
</div>
</div>
</section>
<section id="mean-recap">
<!-- Section title -->
<h2 class="mb-4">MEAN recap</h2>
<div class="row">
<div class="col-md-12">
<p>Here's a quick recap what are the main technologies used in the MEAN stack:</p>
<ul>
<li>
<strong>MongoDB</strong> is a document-oriented NoSQL database meant 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>Angular</strong> is a JavaScript framework that allows you to create very
advanced single-page apps. You can check out how to create an Angular project in
<a href="/docs/standard/cli/getting-started/angular/">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-mean-app">
<!-- Section title -->
<h2 class="mb-4">Building the MEAN 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 - Angular frontend. Let's
create it.
</p>
<h3>Connecting to the API via Angular app</h3>
<p>
We won't show how to create the whole Angular app here. Instead, you'll learn how to
connect to the API <strong>assuming you already have a working app</strong>.
</p>
<p>Here's what we are going to do:</p>
<ul>
<li>Import <code>HttpClientModule</code> in the <code>app/app.module.ts</code> file</li>
<li>Create an <code>TodoApiService</code> that will perform requests</li>
<li>
Demonstrate how to use our service in an example
<code>app/todo-list.component.ts</code> component
</li>
</ul>
<p>So the first thing is quite quick - let's see how to do it</p>
<h4>Importing <code>HttpClientModule</code></h4>
<p>
You surely have the <code>app/app.module.ts</code> file in your app. Open it and do two
things:
</p>
<ul>
<li>
Add the following import at the top of the file:
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="app.module.ts">
import { HttpClientModule } from '@angular/common/http';
</code>
</mdbsnippet>
</li>
<li>
Add <code>HttpClientModule</code> to the <code>imports</code> array like this:
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="app.module.ts">
// ... imports: [ // ... other modules, HttpClientModule ] // ...
</code>
</mdbsnippet>
</li>
</ul>
<p>Now let's create the <code>TodoApiService</code> service.</p>
<h4>Creating <code>TodoApiService</code></h4>
<p>
In order to do that you can make use of the Angular CLI and generate appropriate class.
Just run this command:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="shell" data-name="Shell">
$ ng g s todo-api
</code>
</mdbsnippet>
<p>
It will generate necessary files and add required imports where necessary. Open the
<code>app/todo-api.service.ts</code> file.
</p>
<p>
Next step is to add required imports. In order to do that add the following lines at the
top of the file:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-api.service.ts">
import { HttpClient } from '@angular/common/http'; import { Observable, throwError }
from 'rxjs'; import { catchError, retry } from 'rxjs/operators';
</code>
</mdbsnippet>
<p>Now, inject the module into the constructor like this:</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-api.service.ts">
@Injectable({
providedIn: 'root'
})
export class TodoApiService {
constructor(private http: HttpClient) { }
}
</code>
</mdbsnippet>
<p>
Alright, let's add CRUD operations so that we could talk to our API. Before we do that
let's <code>apiUrl</code> field in our class so that we don't have to repeat it every
time:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-api.service.ts">
@Injectable({
providedIn: 'root'
})
export class TodoApiService {
apiUrl = 'http://express-mongoose-starter.mdbgo.io'; // in your case it will be different
constructor(private http: HttpClient) { }
}
</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="typescript" data-name="todo-api.service.ts">
getTasks() {
return this.http.get(`${this.apiUrl}/tasks`);
}
</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="typescript" data-name="todo-api.service.ts">
addTask(name: string, desc: string) {
return this.http.post(`${this.apiUrl}/tasks`, { name, desc });
}
updateTask(id: number, name: string, desc: string) {
return this.http.put(`${this.apiUrl}/tasks/${id}`, { name, desc });
}
deleteTask(id: number) {
return this.http.delete(`{this.apiUrl}/tasks/${id}`);
}
</code>
</mdbsnippet>
<p>
These are very simple methods that work the same way as the first one - performing POST,
PUT and DELETE requests and returning the result.
</p>
<p>Alright, let's find out how to use this.</p>
<h4>Using the <code>TodoApiService</code> in an example component</h4>
<p>
Assume you have a component named <code>TodoListComponent</code> where you want to display
a list of tasks from the database. You need to make an API request to fetch the real data.
</p>
<p>
This is how you can make use of our service. First, import your service and inject it into
the constructor:
</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-list.component.ts">
import { Component, OnInit } from '@angular/core';
import { TodoApiService } from '../todo-api.service.ts';
@Component({
selector: 'app-todo-list',
templateUrl: './todo-list.component.html',
styleUrls: ['./todo-list.component.scss']
})
export class TodoListComponent implements OnInit {
constructor(private api: TodoApiService) { }
ngOnInit() {
}
}
</code>
</mdbsnippet>
<p>Now you can simply invoke our service method like this:</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-list.component.ts">
ngOnInit() {
this.api.getTasks()
.subscribe((tasks) => {
this.tasks = tasks;
});
}
</code>
</mdbsnippet>
<p>
It will make an API request and assign the result to the <code>tasks</code> field in your
component (we did not show how to create one).
</p>
<p>Making POST, PUT and DELETE requests is just as easy:</p>
<!-- prettier-ignore -->
<mdbsnippet>
<code data-lang="typescript" data-name="todo-list.component.ts">
createTask() {
this.api.addTask('Wash the dishes', 'will take a while')
.subscribe((newTask) => {
this.tasks.push(newTask);
});
}
updateTask() {
const currentTaskId = 1;
this.api.updateTask(currentTaskId, 'Wash the dishes', 'Will take 30 mins')
.subscribe((updatedTask) => {
const oldTask = this.tasks.find((t => t.id === currentTaskId));
oldTask.id = updatedTask.id;
oldTask.name = updatedTask.name;
oldTask.desc = updatedTask.desc;
});
}
deleteTask() {
const currentTaskId = 1;
this.api.deleteTask(currentTaskId)
.subscribe(() => {
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 Angular app.</p>
</div>
</div>
</section>
<!--Section: Docs content-->
</section>
<!--Section: Docs content-->