Translate

12 March 2017

Express JS


Introduction
  • Developed by TJ Holowaychuk in November 2010.
  • Is a web application framework for Node.js.
  • Free and open-source software.
  • Licensed under MIT.

Features
  • Facilitates the rapid development.
  • Allow to set up middleware to respond to HTTP requests.
  • Defines a routing table.
  • Allows to dynamically render HTML pages.

Environment Setup
  • Install node.js and npm
  • Install express module by npm command

--save: Package will appear in your dependencies.

Express Routing
  • Refers how an application responds to a client request to an endpoint.
  • Each route can have one or more handler functions.
app.METHOD(PATH,HANDLER)




Express JS URL Binding


✦req.params used to read the bind values(name,age).
✦regular expression also used to name parameter.


Express JS Middleware
  • Express is middleware web framework that has minimal functionality of its own.
  • An Express application is essentially a series of middleware function calls.
  • Middleware functions access to the request object, the response object, and the next middleware function.

✦app.use() used to Mounts the middleware function or mount to a specified path,the middleware function is executed when the base path matches.
✦The order you place your middleware is very important. Everything will happen in the order that they appear.



Express Router

✦Express router is a class which helps us to create router handlers. By router handler i mean to not just providing routing to our app but also can extend this routing to handle validation, handle 404 or other errors etc.




Express JS Templating

✦Template engine allows you to use static template files in your application.
✦Some popular template engines that work with Express are Pug, Mustache, and EJS.
✦Here, we'll use Pug.

Pug
✦Is a templating engine for express.
✦Earlier it was called Jade.
✦npm command is used to install this package. 


Express Static Files

⊿Are files that clients use as they are from the server.
⊿Files such as images, CSS files, and JavaScript files are served using express.static middleware function.
⊿Express looks up the files relative to the static directory.
app.use(express.static(__dirname+'public'));
⊿We can also set multiple static directories.

No comments:

Post a Comment