+ All Categories
Home > Software > The MEAN stack

The MEAN stack

Date post: 21-Mar-2017
Category:
Upload: nattaya-mairittha
View: 13 times
Download: 1 times
Share this document with a friend
65
NodeJS & AngularJS
Transcript
Page 1: The MEAN stack

NodeJS & AngularJS

Page 2: The MEAN stack

https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.7lpkhdldx

MEAN Stack

Page 3: The MEAN stack

NodeJS

Page 4: The MEAN stack

What is NodeJS?

• Fast (built on Chrome's V8 JavaScript engine)

• FullStack JavaScript

• event-driven, non-blocking I/O

• Realtime Application

Page 5: The MEAN stack

A quick lookvar fs = require('fs');

fs.readFile('hello.txt', function(err, data){ console.log(data.toString()); });

console.log("hello world");

Page 6: The MEAN stack

event-driven

readFile "hello world” data from file

Page 7: The MEAN stack

Callback hell

promise, observable

Page 8: The MEAN stack

Creating Module with CommonJS

• require

• exports

• modules.export

Page 9: The MEAN stack

example: exportsexports.run = function(){ console.log('print'); }

exports.walk = function(){ console.log('copy'); }

var dog = require("./dog");

dog.run();

dog.js app.js

Page 10: The MEAN stack

example: modules.export

module.exports = function(){ console.log("export"); } exports();

exports.js app.js

Page 11: The MEAN stack

Module Type

• Core Module (Built-in Module) => fs , http

• File Module => exports.js

• Folder Module => folder has a package.json

• Third-party Module => npm

Page 12: The MEAN stack

npm install <module_name> npm install -g <module_name>

https://www.npmjs.com/

example: npm i bootstrap 4.0.0, npm -g install forever

NPM

Page 13: The MEAN stack

Setting up the Project

Page 14: The MEAN stack

https://nodejs.org/en/

Install NodeJS

Page 15: The MEAN stack

https://drive.google.com/drive/folders/0B1nLyRIrgh3_dTh5ZFdsNVhHa0E

Seed Project

Page 16: The MEAN stack

npm install

Install Dependencies

Page 17: The MEAN stack

Understanding structure

Page 18: The MEAN stack

Webpack

https://webpack.github.io/docs/configuration.html

Page 19: The MEAN stack

Working with Request and Response

Page 20: The MEAN stack

Request and Responserouter.get('/message/', function (req, res, next) { res.render('node', {message: ‘Hello world’}); });

router.get('/message/:msg', function (req, res, next) { res.render('node', {message: req.params.msg}); });

router.post('/message', function(req, res, next) { var message = req.body.message; res.redirect('/message/' + message); });

Page 21: The MEAN stack

Request and Response

<h1>A NodeJS View</h1> {{ message }}

<form action="/message" method="post"> <input type="text" name="message"> <button type="submit">Submit</button> </form>

Page 22: The MEAN stack

Request

• req.body => http://project/user/5?token=abc

• req.param => http://project/user/5?

• req.query => http://project/user/?token=abc

ref: https://expressjs.com/en/api.html#req

Page 23: The MEAN stack

Data Format

• JSON

• Plain Text

• File

• URL

ref: https://expressjs.com/en/api.html#res

Page 24: The MEAN stack

MongoDB

Page 25: The MEAN stack

https://www.mongodb.com/download-center#community

Install MongoDB

Page 26: The MEAN stack

Stating Mongo Server & Mongo Shell

Page 27: The MEAN stack

Mongoose

• ORM

• Schema

• Validation

ref: https://expressjs.com/en/api.html#res

Page 28: The MEAN stack

Using Mongoose

• Install mongoose

• Connection URI

• Creating model

• Storing data

• Fetching data

ref: https://expressjs.com/en/api.html#res

Page 29: The MEAN stack

Install mongoose

npm install mongoose —save

npm install mongoose-unique-validator —save

ref: https://expressjs.com/en/api.html#res

Page 30: The MEAN stack

Connection URI

mongodb://username:password@hostname:port/database

ex. mongodb://localhost/my-project

Page 31: The MEAN stack

AngularJS

Page 32: The MEAN stack

What is AngularJS?

• Cross Platform ex. Progressive web apps

• Speed and Performance ex. Universal

• Productivity ex. Angular CLI

• Full Development Story ex. Testing, Animation

Page 33: The MEAN stack

Single Page Application

Page 34: The MEAN stack

TypeScript

• Types

• Classes

• Modules

• Decorators

• etc.

https://www.typescriptlang.org/

Page 35: The MEAN stack

Why should I use TypeScript?

Page 36: The MEAN stack

[sudo] npm install typescript -g

Install TypeScript

Page 37: The MEAN stack

Modules

Page 38: The MEAN stack

Modules• NgModule

• declarations

• exports

• imports

• providers

• bootstrap

Page 39: The MEAN stack

Components

Page 40: The MEAN stack

Components

• selector

• templateUrl

• providers

Page 41: The MEAN stack

Staring an Angular

Page 42: The MEAN stack

Interpolation

double-curly braces of interpolation, {{ and }}

Template expressions

<p>The sum of 1 + 1 is not {{1 + 1 + getVal()}}</p>

Page 43: The MEAN stack

Templates and Styles

• inline in the template HTML

• by setting styles or styleUrls metadata

• with CSS imports

Page 44: The MEAN stack

Two-way data binding

<input [(ngModel)]="currentHero.firstName">

Page 45: The MEAN stack

Creating Model

Page 46: The MEAN stack

Multiple Components

reusing components

Page 47: The MEAN stack

Property Binding

[property]="expression".

<img [src]="userImageUrl">

Page 48: The MEAN stack

Property Binding (2)

<user-detail [user]=“currentUser"></user-detail> a custom component (a great way for parent and child components to communicate):

Page 49: The MEAN stack

Data Binding

The binding punctuation: [], () or [()]

Page 50: The MEAN stack

Built-in directives• NgClass

• NgStyle

• NgIf

• NgSwitch

• NgFor

Page 51: The MEAN stack

The asterisk (*) * and <template>

example: Showing an array property with *ngFor

<h1 *ngFor="let student of students”> {{ student.id }} </h1>

Page 52: The MEAN stack

Services

Page 53: The MEAN stack

Creating a Services

@Injectable() decorator

https://angular.io/docs/ts/latest/tutorial/toh-pt4.html

Page 54: The MEAN stack

Lifecycle Hooks

https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html

Page 55: The MEAN stack

Getting message via a Services

Page 56: The MEAN stack

RouterModule

• RouterOutlet

• RouterLink

• RouterLinkActive

• Routes (a configuration)

Page 57: The MEAN stack

Creating a Header

Page 58: The MEAN stack

Setting up Routing

import { RouterModule, Routes } from '@angular/router';

<router-outlet></router-outlet> <!-- Routed views go here -->

Configuration

Page 59: The MEAN stack

Routing and Navigation

Router Links

<a routerLink=“/about">About Us</a>

Page 60: The MEAN stack

HTTP

https://angular.io/docs/ts/latest/tutorial/toh-pt6.html

private productsUrl = 'api/products'; // URL to web api

constructor(private http: Http) { }

getProducts(): Promise<Products[]> { return this.http.get(productsUrl) .toPromise() .then(res => res().data) .catch(this.handleError); }

Page 61: The MEAN stack

Observables

https://github.com/Reactive-Extensions/RxJS

constructor(private http: Http) {}

search(term: string): Observable<any[]> { return this.http .get(`app/products/?name=${term}`) .filter(price => price > 30) .map((res) => res.json().data); }

Page 62: The MEAN stack

Introduction to Angular Testing

Page 63: The MEAN stack

Testing

• Jasmine

• Angular Testing Utilities

• Karma

• Protractor

Page 64: The MEAN stack

https://embed.plnkr.co/?show=preview&show=app%2Fbanner-inline.component.spec.ts

The first component spec

Page 65: The MEAN stack

Large Apps

/app /admin /vendor /public /shared

/auth

components services classes interfaces templates directives


Recommended