Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
nodejs_notes [2018/09/03 08:43]
root [Authentication]
nodejs_notes [2018/10/01 10:17] (current)
root [Express - Chainable Routes]
Line 130: Line 130:
 } }
 </​code>​ </​code>​
 +
 +== Express Framework == 
 +
 +Calling itself a "Fast, un-opinionated,​ minimalist web framework for Node.js"​ http://​expressjs.com/​
 +That proves to be very popular as a stand alone and in other nodejs frameoworks:​
 +
 +=== Routing === 
 +Express exposes itself with http routes, the hello world GET request is:
 +<code javascript>​
 +app.get('/',​ function (req, res) {
 +  res.send('​Hello World!'​)
 +})
 +</​code>​
 +
 +Express allows for regex and wild card routes:
 +<code javascript>​
 +app.get('/​ab?​cd',​...
 +app.get('/​ab+cd'​
 +app.get('/​ab*cd'​
 +</​code>​
 +
 +Route parameters are exposed in a similar way to other frameworks with a colon in the get request url:
 +
 +<code javascript>​
 +app.get('/​users/:​userId/​books/:​bookId',​ function (req, res) {
 +  res.send(req.params)
 +})
 +</​code>​
 +
 +Functions can be chained in the express route:
 +<code javascript>​
 +More than one callback function can handle a route (make sure you specify the next object). For example:
 +
 +<code javascript>​
 +app.get('/​example/​b',​ function (req, res, next) {
 +  console.log('​the response will be sent by the next function ...')
 +  next()
 +}, function (req, res) {
 +  res.send('​Hello from B!')
 +})
 +</​code>​
 +
 +or listed as function calls:
 +
 +<code javascript>​
 +app.get('/​example/​c',​ [cb0, cb1, cb2])
 +</​code>​
 +
 +There are special methods supported to terminate a method call:
 +
 +^ Method ^ Description ^ 
 +| res.download() | Prompt a file to be downloaded.|
 +| res.end() | End the response process. |
 +| res.json() | Send a JSON response. |
 +| res.jsonp() | Send a JSON response with JSONP support. |
 +| res.redirect() | Redirect a request. |
 +| res.render() | Render a view template.|
 +| res.send() | Send a response of various types. |
 +| res.sendFile() | Send a file as an octet stream. | 
 +| res.sendStatus() | Set the response status code and send its string representation as the response body. |
 +
 +=== Express - Chainable Routes === 
 +
 +<code javascript>​
 +app.route('/​book'​)
 +  .get(function (req, res) {
 +    res.send('​Get a random book')
 +  })
 +  .post(function (req, res) {
 +    res.send('​Add a book')
 +  })
 +  .put(function (req, res) {
 +    res.send('​Update the book')
 +  })
 +</​code>  ​
 +
 +=== Express - 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”.
 +
 +Create a router file named birds.js in the app directory, with the following content:
 +
 +<code javascript>​
 +var express = require('​express'​)
 +var router = express.Router()
 +
 +// middleware that is specific to this router
 +router.use(function timeLog (req, res, next) {
 +  console.log('​Time:​ ', Date.now())
 +  next()
 +})
 +// define the home page route
 +router.get('/',​ function (req, res) {
 +  res.send('​Birds home page')
 +})
 +// define the about route
 +router.get('/​about',​ function (req, res) {
 +  res.send('​About birds'​)
 +})
 +
 +module.exports = router
 +</​code>​
 +
 +Then, load the router module in the app:
 +
 +<code javascript>​
 +var birds = require('​./​birds'​)
 +
 +// ...
 +
 +app.use('/​birds',​ birds)
 +</​code> ​
 +
 +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.
  
 === Meteor - Interacting with Data === === Meteor - Interacting with Data ===
 
nodejs_notes.1535989430.txt.gz · Last modified: 2018/09/03 08:43 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd