Express, Moooore
Ahmed Elbassel
Email: elbassel.n13@gmail.com
Skype: ahmed_elbassel
Express, Moooore
1. Passing data to server
2. Exercise
3. Router Responsibility
4. Request package
5. Exercise
6. Express generator
7. Middleware of Express
8. Exercise
1.Passing data to the server
Send as query:
● /users?username=bassel
● To get the data from request: request.query.paramName
1.Passing data to the server
Send as parameter:
● /users/1234
● To get the data from request: request.params.paramName
1.Passing data to the server
Send as the request body as JSON object:
● First, you need to add a body parser in the express app object in app file:
● To get the data from request: request.body
2. Exercise
Write an API to create a new user, delete, get user by id, get all users, and update
user.
Store in a local variable and reply with a success message.
So far no actual actions on the database but later on after MongodDB part it will
be in action.
3. Router Responsibility
- Router has nothing to do with the business at all.
- It is the interface between the Model and view.
- It handles all incoming requests.
- Validations.
4. Request Package
It’s a third party package, is designed to be the simplest way possible to make
http calls. It supports HTTPS and follows redirects by default.
You can find it here: https://www.npmjs.com/package/request
5. Exercise
- Write an API to provide some data about money exchange, get currencies
against US Dollar.
- We will use API to get exchange data and provide it using your app:
http://www.apilayer.net/api/live?
- Access key must be passed as a query:
access_key=24f9e1c515c5ef94185b71f29a39d0db
- Pass the currencies that will be compared: currencies=AUD,EGP,GBP,PLN
6. Express Generator
- The quickest way to get started with express is to utilize the executable
express to generate an application.
- Installing: > npm isntall -g express-generator
- Create express project: > express project-name
7. Middleware of Express
- Middleware functions are functions that have access to the request object
(req), the response object (res), and the next middleware function in the
application’s request-response cycle.
- Middleware functions can perform the following tasks:
- Execute any code.
- Make changes to the request and the response objects.
- End the request-response cycle.
- Call the next middleware function in the stack.
7. Middleware of Express
An Express application can use the following types of middleware:
- Application-level middleware
- Router-level middleware
- Error-handling middleware
- Built-in middleware
- Third-party middleware
- Bind application-level middleware to an instance of the app object by using
the app.use() and app.METHOD() functions.
7.1 Middleware of Express, Application level
7.2 Middleware of Express, Router level
- Router-level middleware works in the same way as application-level
middleware, except it is bound to an instance of express.Router().
7.3 Middleware of Express, Error Handler
- Define error-handling middleware functions in the same way as other
middleware functions, except with four arguments instead of three,
specifically with the signature (err, req, res, next)):
- Starting with version 4.x, Express no longer depends on Connect. With the
exception of express.static, all of the middleware functions that were
previously included with Express’ are now in separate modules.
- express.static is based on serve-static, and is responsible for serving static
assets such as HTML files, images, and so on.
- For example, provide resources in:
- app.use(express.static('public'))
- Then you can get the resources as following:
- http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
7.4 Middleware of Express, Built in Middleware
7.5 Middleware of Express, Third-party middleware
- Use third-party middleware to add functionality to Express apps.
- Install the Node.js module for the required functionality, then load it in your
app at the application level or at the router level.
- The following example illustrates installing and loading the cookie-parsing
middleware function cookie-parser.
Exercise
Develop an app that do some statistics, how many request per the entire
application, how many requests per route?
Develop an API to get the statistics data.
Questions

09 express, moooore

  • 1.
    Express, Moooore Ahmed Elbassel Email:elbassel.n13@gmail.com Skype: ahmed_elbassel
  • 2.
    Express, Moooore 1. Passingdata to server 2. Exercise 3. Router Responsibility 4. Request package 5. Exercise 6. Express generator 7. Middleware of Express 8. Exercise
  • 3.
    1.Passing data tothe server Send as query: ● /users?username=bassel ● To get the data from request: request.query.paramName
  • 4.
    1.Passing data tothe server Send as parameter: ● /users/1234 ● To get the data from request: request.params.paramName
  • 5.
    1.Passing data tothe server Send as the request body as JSON object: ● First, you need to add a body parser in the express app object in app file: ● To get the data from request: request.body
  • 6.
    2. Exercise Write anAPI to create a new user, delete, get user by id, get all users, and update user. Store in a local variable and reply with a success message. So far no actual actions on the database but later on after MongodDB part it will be in action.
  • 7.
    3. Router Responsibility -Router has nothing to do with the business at all. - It is the interface between the Model and view. - It handles all incoming requests. - Validations.
  • 8.
    4. Request Package It’sa third party package, is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default. You can find it here: https://www.npmjs.com/package/request
  • 9.
    5. Exercise - Writean API to provide some data about money exchange, get currencies against US Dollar. - We will use API to get exchange data and provide it using your app: http://www.apilayer.net/api/live? - Access key must be passed as a query: access_key=24f9e1c515c5ef94185b71f29a39d0db - Pass the currencies that will be compared: currencies=AUD,EGP,GBP,PLN
  • 10.
    6. Express Generator -The quickest way to get started with express is to utilize the executable express to generate an application. - Installing: > npm isntall -g express-generator - Create express project: > express project-name
  • 11.
    7. Middleware ofExpress - Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. - Middleware functions can perform the following tasks: - Execute any code. - Make changes to the request and the response objects. - End the request-response cycle. - Call the next middleware function in the stack.
  • 12.
    7. Middleware ofExpress An Express application can use the following types of middleware: - Application-level middleware - Router-level middleware - Error-handling middleware - Built-in middleware - Third-party middleware
  • 13.
    - Bind application-levelmiddleware to an instance of the app object by using the app.use() and app.METHOD() functions. 7.1 Middleware of Express, Application level
  • 14.
    7.2 Middleware ofExpress, Router level - Router-level middleware works in the same way as application-level middleware, except it is bound to an instance of express.Router().
  • 15.
    7.3 Middleware ofExpress, Error Handler - Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature (err, req, res, next)):
  • 16.
    - Starting withversion 4.x, Express no longer depends on Connect. With the exception of express.static, all of the middleware functions that were previously included with Express’ are now in separate modules. - express.static is based on serve-static, and is responsible for serving static assets such as HTML files, images, and so on. - For example, provide resources in: - app.use(express.static('public')) - Then you can get the resources as following: - http://localhost:3000/images/kitten.jpg http://localhost:3000/css/style.css http://localhost:3000/js/app.js http://localhost:3000/images/bg.png http://localhost:3000/hello.html 7.4 Middleware of Express, Built in Middleware
  • 17.
    7.5 Middleware ofExpress, Third-party middleware - Use third-party middleware to add functionality to Express apps. - Install the Node.js module for the required functionality, then load it in your app at the application level or at the router level. - The following example illustrates installing and loading the cookie-parsing middleware function cookie-parser.
  • 18.
    Exercise Develop an appthat do some statistics, how many request per the entire application, how many requests per route? Develop an API to get the statistics data.
  • 19.