Backend
Backend
Backend Services
Part 1: Node JS and MySQL
Jumail Bin Taliba
School of Computing, UTM
June 2020
Watch on YouTube
Set the playback speed 1.5X
See the timestamp in the description
• Design Principles and Design
Patterns
• Local Development
• Setting Up Databases
Agenda • Writing Code for REST API
• Deployment
• Testing with Flutter App
SOLID Principles
Why care?
• Software development is a collaborative work
• The intention of the principles is to make software easier to maintain
and to extend
Design Patterns
Programming:
Node, PHP, Python,
Perl,
Deno
Database:
MySQL, MongoDB,
Firebase
HTTP
REST
MVVM GraphQL MVC
Architecture Example: Get the profile of a given user Architecture
HTTP : http://www.mywebsite.com/getprofile.php? uid=1213
REST : GET http://www.mywebsite.com/profiles/1213
GraphQL: query{
User( uid: 1213){
fullName
role
}
}
Back-end Architecture
Front-end Back-end
Model
1. Client Request • Handles data related logic
Controller
• Talks to the database, e.g
SELECT, INSERT, UPDATE, etc
• Handles client
requests: GET, POST,
etc
Front-end Back-end
Model
user = UserModel.getUser(10)
router.get(‘/user/:id’)
GET /users/10
Controller Model
Router
user = UserModel.getUser(10)
backend
https://github.com/jumail-utm/backend_node_mysql
https://github.com/jumail-utm/flutter_todo_rest
Setting Up for Local Development
• Install Xampp
https://www.apachefriends.org/download.html
MySQL Workbench
https://www.mysql.com/products/workbench
• Install Node JS
https://nodejs.org/en/download
Creating Database
https://github.com/jumail-
utm/backend_node_mysql/blob/master/dev/mysql/setup_database.sql
Writing Code for REST API Service with Node JS
• Setup Project
• Project structure
• Install dependency packages
• Setup Database connection
• Define model classes
• Define route handlers
Middleware
Middleware
Middleware
HTTP Response
Express JS Middleware (2)
Terminologies
Example express app code
HTTP const app = express()
method Route
app.get( '/', handleRootRoute )
Router
app.post( '/users', handleCreateUser )
app.get(‘/users/:id’)
Model app.get( '/users/:id', (req, res, next ) => {
// code are excluded for brevity
HTTP Request app.post(‘/users’)
Model })
Middleware
Middleware
function handleCreateUser(req, res, next){
// code are excluded for brevity
}
HTTP Response
Express JS Middleware (3)
How it works?
app.use()
Model app.get(‘/endpoint’)
Model
HTTP express.json()
app.post(‘/’)
Middleware
Request Model
Router app.get(‘/’)
Model
Middleware
Middleware
Middleware
next() next()
HTTP
Response
Express JS Middleware (4)
Middleware Functions
const app = express()
Each middleware function accepts three parameters
app.post( '/users', handleCreateUser )
• req: Request data from the client
app.get( '/users/:id', (req, res, next ) => {
• res: Server response to the client // code are excluded for brevity
})
• next: function to execute the next middleware
function handleCreateUser(req, res, next){
// code are excluded for brevity
}
Express JS Middleware (5)
• It promotes modularity
HTTP Request
verifyAdminRole
isUserLoggedIn
newAccount
• Large tasks can be splitted into
smallers ones
HTTP Response
Express JS Middleware (7)
backend
https://github.com/jumail-utm/backend_node_mysql
https://github.com/jumail-utm/flutter_todo_rest
• Setting up databases
• MVC pattern for REST API
Summary • Writing REST API service with
Node JS and MySQL
• Deploy Node apps to Heroku
Integrating with Backend
Backend Services
Part 2: REST Service on Firebase
Jumail Bin Taliba
School of Computing, UTM
June 2020
Watch on YouTube
Set the playback speed 1.5X
See the timestamp in the description
• Architecture Setups
• Introduction to Firebase
• Setting Up Local Firebase
Agenda • Developing REST Service
• Deploying to Firebase
Introduction
Architecture Setups
Front-end Back-end
Business Logics
Authentication
Authorization
Database
Architecture Setups (2)
Example Setup 1.2: Thin Client
Front-end Back-end
Business Logics
Database access
User Interface
SDK
UI Logic Related
Authentication
Database Authorization
Architecture Setups (3)
Example Setup 2.1: Thick Client e.g. desktop apps, Java apps,
.Net apps, Firebase apps,
mobile apps, etc
Front-end Back-end
SDK
Database
Business logics
User Interface
UI Logic Related
Authentication
Authorization
Architecture Setups (3)
Example Setup 2.2: Thick Client
Front-end Back-end
Authentication
SDK
Database Authorization
Business logics
User Interface
UI Logic Related
Architecture Setups (4)
Node JS
Database access
Authentication
Default Credential
User Interface REST Firebase Admin SDK
Authorization
UI Logic Related
Front-end Back-end
Firebase Admin
Storage
Credential
Account
SDK
Firestore
Business Logics
Architecture Setups (5)
Firestore Firebase
Security Rules
Firebase Config
Firebase SDK
Example of
Firebase Security
Rules
Architecture Setups (7)
Front-end Back-end
Example use case:
Cloud Functions
Refactoring code (Node JS)
Business Logics
Firebase SDK
Firebase Config
Default Credential
Firebase Admin SDK
User Interface
UI Logic Related Firestore Storage Authentication
Business Logics
Firebase Security Rules
Introduction to Firebase
• What is Firebase?
• A BaaS solution
• Backend made easy
• Firebase Services:
• Database: Firestore, Realtime (document-based NoSQL)
• Cloud Storage
• Authentication
• Authorization
• Web Hosting
• Cloud Functions
• Cloud Messaging
• Many more ….
What is NoSQL?
11 Abdullah [email protected] {
"uid" : 11, Document 2: uid 53
Razali "name" : "Abdullah Razali",
{
53 Ali Bakar [email protected] "email" : "[email protected]"
"uid" : 53, Document 3: uid 211
} "name" : "Ali{ Bakar",
211 Siti Aminah [email protected]
"email" : "[email protected]"
"uid" : 211,
Rashid } "name" : "Siti Aminah Rashid",
"email" : "[email protected]"
Example query: }
• Install firebase-tools
Example 1: Triggers
Example 2:
Callable Functions
A callable function can be called
directly from the client app.
Example 3:
HTTP Requests
Functions are called from
HTTP-based clients, e.g.
web browser, REST client
Demo
Developing REST Service on Firebase
Demo - Project Source Code
backend
https://github.com/jumail-utm/backend_firebase_rest
router.get(‘/user/:id’)
GET /users/10
Controller Model
Router
user = UserModel.getUser(10)
Middleware
Middleware
Middleware
HTTP Response
Watch the previous lecture video to learn more about Express JS and Router
Setting Up Local Firebase
Developing REST API
Project Structure
Deploying the Project
• Introduction to Firebase
• Set up Local Firebase
Summary • Develop REST API on Firebase
• Deploy REST API to Firebase