EXPRESS.JS
YAUHENI NIKANOVICH
What Express.js is?
‣ Express is a minimal and flexible web application
framework for NodeJS what offers an extensive set of
features for web and mobile applications.
‣ Express app, in fact, is a series of calls to middleware
functions
What is middleware function?
The function of the middleware are functions that have access to the
request object (req), response object (res) to the following functions
of the intermediate processing in the cycle “request-response”
applications.
The following middleware, usually denoted by the variable next.
Middleware functions can perform the following tasks:
‣ The execution of any code.
‣ Changes to the object requests and responses.
‣ The end of cycle “request-response”.
‣ The following function call middleware from the stack.
Express application can use the following types of middleware:
‣ Application Middleware level
‣ Router Middleware level
‣ Error handling Middleware level
‣ Built-in Middleware handlers
‣ Third-party middleware
Application Middleware level (1/4)
We can associate middleware-level application instance of
the application object by using the function:
‣ app.use()
‣ app.METHOD() // Where METHOD is the HTTP method
This function will be executed every time the application receives the request
In following example, the route and its processing function
are presented (the system of intermediate handlers). This
function processes GET requests addressed to resources for
path/user/:id path
Application Middleware level (2/4)
Application Middleware level (3/4)
In order to skip the remaining functions additional processing
in the middleware stack of a router, invoke next('route') to
transfer control to the next route.
NOTE: next('route') only works in the middleware functions,
loaded with features app.METHOD() or router.METHOD().
Following example shows an auxiliary stack middleware
to process GET requests addressed to resources in the
path /user/:id
Application Middleware level (4/4)
Router Middleware level (1/3)
Middleware-level router is the same as the middleware
application level, but it is bound to an instance of
express.Router()
Use the express.Router class to create modular, mountable
route handlers. A Router instance is a complete middleware
and routing system; for this reason, it is often referred to as a
“mini-app”.
Router Middleware level (2/3)
The following example creates a router as a module, loads a
middleware function in it, defines some routes, and mounts
the router module on a path in the main app.
Create a router file named birds.js in the app directory
Router Middleware level (3/3)
Then, load the router module in the app
The app will now be able to handle requests to /birds
and /birds/about, as well as call the timeLog middleware
function that is specific to the route.
Error handling Middleware level
Definition of a functions of middleware for error handling as
well as other functions of the middleware, but indicating not
three, but four arguments: (err, req, res, next)
Built-in Middleware handlers
The only built-in middleware function in Express is
express.static. This function is based on serve-static, and is
responsible for serving static assets such as HTML files,
images, and so on.
The function signature is: express.static(root, [options])
Built-in Middleware handlers
Here is an example of using the
express.static middleware function
with an elaborate options object
You can have more than one static
directory per app
Third-party middleware
Use third-party middleware to add functionality to Express apps.
Load it by using app.use
YAUHENI NIKANOVICH
Thanks!
EMAIL: zhenyanikon@gmail.com
2016

Expressjs

  • 1.
  • 2.
    What Express.js is? ‣Express is a minimal and flexible web application framework for NodeJS what offers an extensive set of features for web and mobile applications. ‣ Express app, in fact, is a series of calls to middleware functions
  • 3.
    What is middlewarefunction? The function of the middleware are functions that have access to the request object (req), response object (res) to the following functions of the intermediate processing in the cycle “request-response” applications. The following middleware, usually denoted by the variable next.
  • 4.
    Middleware functions canperform the following tasks: ‣ The execution of any code. ‣ Changes to the object requests and responses. ‣ The end of cycle “request-response”. ‣ The following function call middleware from the stack.
  • 5.
    Express application canuse the following types of middleware: ‣ Application Middleware level ‣ Router Middleware level ‣ Error handling Middleware level ‣ Built-in Middleware handlers ‣ Third-party middleware
  • 6.
    Application Middleware level(1/4) We can associate middleware-level application instance of the application object by using the function: ‣ app.use() ‣ app.METHOD() // Where METHOD is the HTTP method This function will be executed every time the application receives the request
  • 7.
    In following example,the route and its processing function are presented (the system of intermediate handlers). This function processes GET requests addressed to resources for path/user/:id path Application Middleware level (2/4)
  • 8.
    Application Middleware level(3/4) In order to skip the remaining functions additional processing in the middleware stack of a router, invoke next('route') to transfer control to the next route. NOTE: next('route') only works in the middleware functions, loaded with features app.METHOD() or router.METHOD().
  • 9.
    Following example showsan auxiliary stack middleware to process GET requests addressed to resources in the path /user/:id Application Middleware level (4/4)
  • 10.
    Router Middleware level(1/3) Middleware-level router is the same as the middleware application level, but it is bound to an instance of express.Router() Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.
  • 11.
    Router Middleware level(2/3) The following example creates a router as a module, loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app. Create a router file named birds.js in the app directory
  • 12.
    Router Middleware level(3/3) Then, load the router module in the app The app will now be able to handle requests to /birds and /birds/about, as well as call the timeLog middleware function that is specific to the route.
  • 13.
    Error handling Middlewarelevel Definition of a functions of middleware for error handling as well as other functions of the middleware, but indicating not three, but four arguments: (err, req, res, next)
  • 14.
    Built-in Middleware handlers Theonly built-in middleware function in Express is express.static. This function is based on serve-static, and is responsible for serving static assets such as HTML files, images, and so on. The function signature is: express.static(root, [options])
  • 15.
    Built-in Middleware handlers Hereis an example of using the express.static middleware function with an elaborate options object You can have more than one static directory per app
  • 16.
    Third-party middleware Use third-partymiddleware to add functionality to Express apps. Load it by using app.use
  • 17.