SlideShare a Scribd company logo
1 of 22
Download to read offline
Workshop’s repository: https://goo.gl/cjR7EL
Learn Backend JavaScript
Workshop with sample
repository.
Workshop’s repository: https://goo.gl/cjR7EL
About me
● Tsuyoshi Maeda
● A student of Code Chrysalis.
(Freelance engineer)
● I started programming at 21
years. (Junior university student.)
● What I like
○ Playing futsal (soccer)
○ Tsukemen (dipping noodles)
○ Watching video games on youtube
and Niconico.
Workshop’s repository: https://goo.gl/cjR7EL
After this meetup
● You will
○ Understand back-end programming with JavaScript.
○ Be able to set up back-end environment.
■ DB
■ Web Server
○ Create API server.
○ Be able to become a full-stack engineer. (If you are already familiar
with front-end JavaScript.)
Workshop’s repository: https://goo.gl/cjR7EL
Outline
● Advantages of JavaScript as a backend.
● Database with JavaScript
● Web server with JavaScript
● Create a API server together. (Workshop)
Today’s Repository: https://goo.gl/cjR7EL
Workshop’s repository: https://goo.gl/cjR7EL
Advantages of JavaScript as a backend.
● Don’t have to switch your brain between Front-end and Back-end.
● For web front engineers, They can start Back-end programming without
learning new programming language.
● Shortest way to become a full stack engineer. (Learning only one
programming language.)
Workshop’s repository: https://goo.gl/cjR7EL
Disadvantages of JavaScript as a backend.
● Many knowledge of back-end JavaScript are not matured compared to
other server side language.
○ NodeJS: 2009.
■ JavaScript was born on 1995 but it is not for server-side.
■ JavaScript for browser cannot handle server resources.)
○ Ruby on Rails: 2004. (Ruby: 1995)
○ PHP: 1995
■ Ruby and PHP are for server-side. (Then can handle server resources.)
● Tend to grow faster and come out new information.
○ It is hard to follow new knowledge.
Workshop’s repository: https://goo.gl/cjR7EL
Database with JavaScript
● Driver/Adapter
○ “pg “ for PostgreSQL
○ “mysql” for MySQL
○ “sqlite3” for SQLite3
● ORM (Object-relational mapping)
○ Sequelize (Most numerous of downloads and stars on Github.)
○ Bookshelf with knex (Second largest numerous of downloads and stars.)
○ https://npmcompare.com/compare/bookshelf,knex,objection,orm,sequelize
Workshop’s repository: https://goo.gl/cjR7EL
Driver/Adapter
Web Server DB
pg
mysql
sqlite3
driver/adapter
Return raw data
Communicate with DB like client
on shell.
Workshop’s repository: https://goo.gl/cjR7EL
ORM
Web
Server
DB
pg
mysql
sqlite3
connector/adapter
Return raw data
Communicate with DB like client
on shell.
ORM
● Return raw data
● Convert to data that
is easy to
manipulate.
Workshop’s repository: https://goo.gl/cjR7EL
ORM
● Instead of using raw SQL, ORM offers some methods that translate into
SQL.
○ e.g.) Code snippets of Sequelize to manipulate Todos table.
■ Todo.findAll({...}) => SELECT ...
■ Todo.create({...}) => INSERT ..., UPDATE ...
■ Todo.findAll({include: [{model: ‘User’}...]}) => Join
■ and so on …
● You don’t have to care about each dialect of DB.
● Just change adapter when you want to change DB.
Workshop’s repository: https://goo.gl/cjR7EL
Web server with JavaScript
● Express (Most popular node http framework and we will use this for
today’s workshop.)
○ Middlewares
○ Router
● Other JavaScript web frameworks
○ Socket.io
○ Hapi.js
○ Sails.js
Workshop’s repository: https://goo.gl/cjR7EL
Middleware
● Middleware converts request to what you can handle easily.
○ Check if user is logged in or not.
○ Log HTTP requests
○ Parse request body of Json to object of JavaScript.
● The Express middleware modules maintained by the Expressjs team.
○ https://expressjs.com/en/resources/middleware.html
Workshop’s repository: https://goo.gl/cjR7EL
Web Server
Middleware
DBClient
Middleware
process 1
process 2
...
process n-1
router
Connect to DB if a process has a code
that communicate with DB.
Request /api/data
Response /api/data
Workshop’s repository: https://goo.gl/cjR7EL
Middleware
● Example code of middleware
// req: Includes req object and can modify req object
// res: Includes res object
// next: callback function to call next middleware or router.
function(req, res, next) {
req.body = …; // You can add any properties
res.statusCode = …;
// If you want to stop this process, do not call `next` callback.
if( condition ) return false;
next();
}
Workshop’s repository: https://goo.gl/cjR7EL
● Router handle each request and direct the request to proper process.
○ GET /path/to/something
● You can map one URL to each HTTP methods.
○ “GET” for getting data.
○ “POST” for creating new data.
○ “PUT” for updating stored data.
○ “DELETE” for deleting stored data.
● CRUD
○ Create, Read, Update, Delete
Router
Workshop’s repository: https://goo.gl/cjR7EL
Web Server
Router
DBClient
Connect to DB if a process(controller)
has a code that communicate with DB.
Router
GET
POST
PUT
DELETE
Controller
fetchData
createData
updateData
deleteData
Request /api/data
Response /api/data
Mapping request to controller/handler by
HTTP methods.
Workshop’s repository: https://goo.gl/cjR7EL
Router, Controller
● Example code of router
const router = require('express').Router();
const controller = require('./api.controller');
router.route('/todos')
.get(controller.getTodos)
.post(controller.postTodo)
.put(controller.putTodo)
.delete(controller.deleteTodo);
● Example code of Controller
module.exports = {
getTodos(req, res) {...},
postTodos(req, res) {...},
putTodos(req, res) {...},
deleteTodos(req, res) {...}
};
Workshop’s repository: https://goo.gl/cjR7EL
Web Server
Middleware and Router
DB
Client
Router
GET
POST
PUT
DELETE
Controller
fetchData
createData
updateData
deleteData
Request:
GET /api/data
Response:
GET /api/data
Middleware
process 1
process 2
...
Workshop’s repository: https://goo.gl/cjR7EL
DB
Web Server
Client
Router
GET
POST
PUT
DELETE
Controller
fetchData
createDat
a
updateDat
a
deleteDat
a
Middleware
process 1
process 2
...
pg
mysql
sqlite3
ORM
ORM, Middleware and Router
Workshop’s repository: https://goo.gl/cjR7EL
● DB
○ What is a driver/adapter?
○ What is a ORM?
● Web server
○ What is a middleware?
○ What is a router?
○ When user request to server, how does the data flow works?
Recap
Workshop’s repository: https://goo.gl/cjR7EL
Let’s create a API server together!!!
https://goo.gl/cjR7EL
Workshop time
Workshop’s repository: https://goo.gl/cjR7EL
My contacts
● Twitter: @duyoji
● Github: duyoji
● Blog
○ http://duyoji.hatenablog.com/ (Written in Japanese and not only Tech blog.)
○ http://tsuyoshi-maeda.hatenablog.com/ (Written in English and only Tech blog.)

More Related Content

What's hot

Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodb
Lee Theobald
 
Video WebChat Conference Tool
Video WebChat Conference ToolVideo WebChat Conference Tool
Video WebChat Conference Tool
Sergiu Gordienco
 

What's hot (20)

Big mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-mungeBig mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-munge
 
Grooscript and Grails 3
Grooscript and Grails 3Grooscript and Grails 3
Grooscript and Grails 3
 
MLBlock
MLBlockMLBlock
MLBlock
 
Indexes don't mean slow inserts.
Indexes don't mean slow inserts.Indexes don't mean slow inserts.
Indexes don't mean slow inserts.
 
Introduction tomongodb
Introduction tomongodbIntroduction tomongodb
Introduction tomongodb
 
Puppet Camp Ghent 2013
Puppet Camp Ghent 2013Puppet Camp Ghent 2013
Puppet Camp Ghent 2013
 
Introduction to Gulp
Introduction to GulpIntroduction to Gulp
Introduction to Gulp
 
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
 
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
 
Arctic stack
Arctic stackArctic stack
Arctic stack
 
Creating ASTTs The painful truth
Creating ASTTs The painful truthCreating ASTTs The painful truth
Creating ASTTs The painful truth
 
PHP and node.js Together
PHP and node.js TogetherPHP and node.js Together
PHP and node.js Together
 
What Reika Taught us
What Reika Taught usWhat Reika Taught us
What Reika Taught us
 
Qtp word methods
Qtp word methodsQtp word methods
Qtp word methods
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
იოსებ ძმანაშვილი Node.js
იოსებ ძმანაშვილი   Node.jsიოსებ ძმანაშვილი   Node.js
იოსებ ძმანაშვილი Node.js
 
Not only SQL
Not only SQL Not only SQL
Not only SQL
 
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupДоклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
 
Video WebChat Conference Tool
Video WebChat Conference ToolVideo WebChat Conference Tool
Video WebChat Conference Tool
 
Pgconf asia-201612203-pg reversi
Pgconf asia-201612203-pg reversiPgconf asia-201612203-pg reversi
Pgconf asia-201612203-pg reversi
 

Similar to Learn backend java script

Tech meetup: Web Applications Performance
Tech meetup: Web Applications PerformanceTech meetup: Web Applications Performance
Tech meetup: Web Applications Performance
Santex Group
 

Similar to Learn backend java script (20)

Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performance
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
 
Tech meetup: Web Applications Performance
Tech meetup: Web Applications PerformanceTech meetup: Web Applications Performance
Tech meetup: Web Applications Performance
 
Building an analytics workflow using Apache Airflow
Building an analytics workflow using Apache AirflowBuilding an analytics workflow using Apache Airflow
Building an analytics workflow using Apache Airflow
 
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018Beyond Wordcount  with spark datasets (and scalaing) - Nide PDX Jan 2018
Beyond Wordcount with spark datasets (and scalaing) - Nide PDX Jan 2018
 
BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013
 
Xephon K A Time series database with multiple backends
Xephon K A Time series database with multiple backendsXephon K A Time series database with multiple backends
Xephon K A Time series database with multiple backends
 
Benchx: An XQuery benchmarking web application
Benchx: An XQuery benchmarking web application Benchx: An XQuery benchmarking web application
Benchx: An XQuery benchmarking web application
 
React starter-kitでとっとと始めるisomorphic開発
React starter-kitでとっとと始めるisomorphic開発React starter-kitでとっとと始めるisomorphic開発
React starter-kitでとっとと始めるisomorphic開発
 
Patroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easyPatroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easy
 
Accumulo Summit Keynote 2018
Accumulo Summit Keynote 2018Accumulo Summit Keynote 2018
Accumulo Summit Keynote 2018
 
Monitoring Kafka w/ Prometheus
Monitoring Kafka w/ PrometheusMonitoring Kafka w/ Prometheus
Monitoring Kafka w/ Prometheus
 
Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018
 
Treinamento frontend
Treinamento frontendTreinamento frontend
Treinamento frontend
 
Catalyst MVC
Catalyst MVCCatalyst MVC
Catalyst MVC
 
Revealing ALLSTOCKER
Revealing ALLSTOCKERRevealing ALLSTOCKER
Revealing ALLSTOCKER
 
202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUP202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUP
 
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
 
Drools & jBPM Workshop London 2013
Drools & jBPM Workshop London 2013Drools & jBPM Workshop London 2013
Drools & jBPM Workshop London 2013
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 

Learn backend java script

  • 1. Workshop’s repository: https://goo.gl/cjR7EL Learn Backend JavaScript Workshop with sample repository.
  • 2. Workshop’s repository: https://goo.gl/cjR7EL About me ● Tsuyoshi Maeda ● A student of Code Chrysalis. (Freelance engineer) ● I started programming at 21 years. (Junior university student.) ● What I like ○ Playing futsal (soccer) ○ Tsukemen (dipping noodles) ○ Watching video games on youtube and Niconico.
  • 3. Workshop’s repository: https://goo.gl/cjR7EL After this meetup ● You will ○ Understand back-end programming with JavaScript. ○ Be able to set up back-end environment. ■ DB ■ Web Server ○ Create API server. ○ Be able to become a full-stack engineer. (If you are already familiar with front-end JavaScript.)
  • 4. Workshop’s repository: https://goo.gl/cjR7EL Outline ● Advantages of JavaScript as a backend. ● Database with JavaScript ● Web server with JavaScript ● Create a API server together. (Workshop) Today’s Repository: https://goo.gl/cjR7EL
  • 5. Workshop’s repository: https://goo.gl/cjR7EL Advantages of JavaScript as a backend. ● Don’t have to switch your brain between Front-end and Back-end. ● For web front engineers, They can start Back-end programming without learning new programming language. ● Shortest way to become a full stack engineer. (Learning only one programming language.)
  • 6. Workshop’s repository: https://goo.gl/cjR7EL Disadvantages of JavaScript as a backend. ● Many knowledge of back-end JavaScript are not matured compared to other server side language. ○ NodeJS: 2009. ■ JavaScript was born on 1995 but it is not for server-side. ■ JavaScript for browser cannot handle server resources.) ○ Ruby on Rails: 2004. (Ruby: 1995) ○ PHP: 1995 ■ Ruby and PHP are for server-side. (Then can handle server resources.) ● Tend to grow faster and come out new information. ○ It is hard to follow new knowledge.
  • 7. Workshop’s repository: https://goo.gl/cjR7EL Database with JavaScript ● Driver/Adapter ○ “pg “ for PostgreSQL ○ “mysql” for MySQL ○ “sqlite3” for SQLite3 ● ORM (Object-relational mapping) ○ Sequelize (Most numerous of downloads and stars on Github.) ○ Bookshelf with knex (Second largest numerous of downloads and stars.) ○ https://npmcompare.com/compare/bookshelf,knex,objection,orm,sequelize
  • 8. Workshop’s repository: https://goo.gl/cjR7EL Driver/Adapter Web Server DB pg mysql sqlite3 driver/adapter Return raw data Communicate with DB like client on shell.
  • 9. Workshop’s repository: https://goo.gl/cjR7EL ORM Web Server DB pg mysql sqlite3 connector/adapter Return raw data Communicate with DB like client on shell. ORM ● Return raw data ● Convert to data that is easy to manipulate.
  • 10. Workshop’s repository: https://goo.gl/cjR7EL ORM ● Instead of using raw SQL, ORM offers some methods that translate into SQL. ○ e.g.) Code snippets of Sequelize to manipulate Todos table. ■ Todo.findAll({...}) => SELECT ... ■ Todo.create({...}) => INSERT ..., UPDATE ... ■ Todo.findAll({include: [{model: ‘User’}...]}) => Join ■ and so on … ● You don’t have to care about each dialect of DB. ● Just change adapter when you want to change DB.
  • 11. Workshop’s repository: https://goo.gl/cjR7EL Web server with JavaScript ● Express (Most popular node http framework and we will use this for today’s workshop.) ○ Middlewares ○ Router ● Other JavaScript web frameworks ○ Socket.io ○ Hapi.js ○ Sails.js
  • 12. Workshop’s repository: https://goo.gl/cjR7EL Middleware ● Middleware converts request to what you can handle easily. ○ Check if user is logged in or not. ○ Log HTTP requests ○ Parse request body of Json to object of JavaScript. ● The Express middleware modules maintained by the Expressjs team. ○ https://expressjs.com/en/resources/middleware.html
  • 13. Workshop’s repository: https://goo.gl/cjR7EL Web Server Middleware DBClient Middleware process 1 process 2 ... process n-1 router Connect to DB if a process has a code that communicate with DB. Request /api/data Response /api/data
  • 14. Workshop’s repository: https://goo.gl/cjR7EL Middleware ● Example code of middleware // req: Includes req object and can modify req object // res: Includes res object // next: callback function to call next middleware or router. function(req, res, next) { req.body = …; // You can add any properties res.statusCode = …; // If you want to stop this process, do not call `next` callback. if( condition ) return false; next(); }
  • 15. Workshop’s repository: https://goo.gl/cjR7EL ● Router handle each request and direct the request to proper process. ○ GET /path/to/something ● You can map one URL to each HTTP methods. ○ “GET” for getting data. ○ “POST” for creating new data. ○ “PUT” for updating stored data. ○ “DELETE” for deleting stored data. ● CRUD ○ Create, Read, Update, Delete Router
  • 16. Workshop’s repository: https://goo.gl/cjR7EL Web Server Router DBClient Connect to DB if a process(controller) has a code that communicate with DB. Router GET POST PUT DELETE Controller fetchData createData updateData deleteData Request /api/data Response /api/data Mapping request to controller/handler by HTTP methods.
  • 17. Workshop’s repository: https://goo.gl/cjR7EL Router, Controller ● Example code of router const router = require('express').Router(); const controller = require('./api.controller'); router.route('/todos') .get(controller.getTodos) .post(controller.postTodo) .put(controller.putTodo) .delete(controller.deleteTodo); ● Example code of Controller module.exports = { getTodos(req, res) {...}, postTodos(req, res) {...}, putTodos(req, res) {...}, deleteTodos(req, res) {...} };
  • 18. Workshop’s repository: https://goo.gl/cjR7EL Web Server Middleware and Router DB Client Router GET POST PUT DELETE Controller fetchData createData updateData deleteData Request: GET /api/data Response: GET /api/data Middleware process 1 process 2 ...
  • 19. Workshop’s repository: https://goo.gl/cjR7EL DB Web Server Client Router GET POST PUT DELETE Controller fetchData createDat a updateDat a deleteDat a Middleware process 1 process 2 ... pg mysql sqlite3 ORM ORM, Middleware and Router
  • 20. Workshop’s repository: https://goo.gl/cjR7EL ● DB ○ What is a driver/adapter? ○ What is a ORM? ● Web server ○ What is a middleware? ○ What is a router? ○ When user request to server, how does the data flow works? Recap
  • 21. Workshop’s repository: https://goo.gl/cjR7EL Let’s create a API server together!!! https://goo.gl/cjR7EL Workshop time
  • 22. Workshop’s repository: https://goo.gl/cjR7EL My contacts ● Twitter: @duyoji ● Github: duyoji ● Blog ○ http://duyoji.hatenablog.com/ (Written in Japanese and not only Tech blog.) ○ http://tsuyoshi-maeda.hatenablog.com/ (Written in English and only Tech blog.)