express route

  • url
  • http method supported
    • get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, and connect
    • app.all() apply to all the method
  • app.use('/api/offer',offer);
    • everything in offer will have /api/offer in the beginning of the url
    • within the route js for offer, do not need ‘get(“/api/offer/xxx”,xxx)’, otherwise the route path will be api/offer/api/offer/xxx, can just be ‘get(“/getOffer”,xxx)’
  • route path
    • support the regular expression
    • This route path will match /abe and /abcde.
      app.get('/ab(cd)?e', function (req, res) {
  • route parameter
    • specify the route parameters in the path of the route as shown below.
      app.get('/users/:userId/books/:bookId', function (req, res) {
    • – and . are interpreted literally
      Route path: /flights/:from-:to
      Request URL: http://localhost:3000/flights/LAX-SFO
      req.params: { "from": "LAX", "to": "SFO" }

      Route path: /plantae/:genus.:species Request URL: http://localhost:3000/plantae/Prunus.persica req.params: { "genus": "Prunus", "species": "persica" }

    • get url parameter doesn’t need to be listed in ‘app.get(“xxx”‘, it can be accessed directly from req.query.
  • route handler
    • single callback function
    • More than one callback function can handle a route (make sure you specify the next object). For example:
      app.get('/example/b', function (req, res, next) {
        console.log('the response will be sent by the next function ...')
      }, function (req, res) {
        res.send('Hello from B!')
    • An array of callback functions can handle a route
    • A combination of independent functions and arrays of functions can handle a route.
  • response method
    • the methods on the response object (res) in the following table can send a response to the client, and terminate the request-response cycle.
    • If none of these methods are called from a route handler, the client request will be left hanging.
      Method Description 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.
  • app.route()

    You can create chainable route handlers for a route path by using app.route()

    • why need this chainable stuff?
    • does it mean that 1 request will go through all of them?
      • only the right method will be triggered?
      • ie. a supplementary to app.all?
This entry was posted in Nodejs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s