expres路由

basic route

const express=require(‘express’);
var app=express();
app.methds(“path”,(req,res)=>{});
每个路由可以包含next(),相当于一个callback的参数,作用是切换当前控制到下一个callback

methods的种类:

  1. app.get("/",(req,res)=>{})
  2. app.post("/",(req,res)=>{})
  3. app.put("/",(req,res)=>{})
  4. app.delete("/",(req,res)=>{});
  5. 一个特殊的路由method——app.use(),是用来导入中间件的function作为所有的http方式的path,支持上述四种请求方式以及其他任何请求方式
app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...')
  next() // pass control to the next handler
})

路由中的路径格式

支持string、正则表达式、string patterns
?、+、*、()可以看做是正则表达式的子集,这些符号最后将url字符串逐路径解析为-或者是dot(.),如果使用 符 号 , 则 最 好 使 用 ( [ ] ) 将 他 包 围 , e g . p a t h 为 / d a t a / 符号,则最好使用([])将他包围,eg.path为/data/ 使([])eg.path/data/book应该写为/data/([$])book

app.get('/ab(cd)?e', function (req, res) {
  res.send('ab(cd)?e');//This route path will match /abe and /abcde.
})
app.get(/.*fly$/, function (req, res) {
  res.send('/.*fly$/');//This route path will match butterfly and dragonfly, but not butterflyman, dragonflyman, and so on.
})

路由params

app.get(’/users/:userId/books/:bookId’, function (req, res) {
res.send(req.params)//
})
上面的可以解释为:
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { “userId”: “34”, “bookId”: “8989” }

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” }

Route path: /user/:userId(\d+)
Request URL: http://localhost:3000/user/42
req.params: {“userId”: “42”}

路由处理程序

1.可以像中间件一样使用多个callback处理一个请求。

app.get("/",(req,res)=>{
console.log("即将进入nex");
next()},
(req,res)=>{
 res.send('Hello from B!')
});
  1. callback可以是一个function数组

    var cb0 = function (req, res, next) {
    console.log(‘CB0’)
    next()
    }

    var cb1 = function (req, res, next) {
    console.log(‘CB1’)
    next()
    }

    var cb2 = function (req, res) {
    res.send(‘Hello from C!’)
    }

    app.get(’/example/c’, [cb0, cb1, cb2])

  2. 可以是一个独立的function和function数组组成

var cb0 = function (req, res, next) {
  console.log('CB0')
  next()
}

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

app.get('/example/d', [cb0, cb1], function (req, res, next) {
  console.log('the response will be sent by the next function ...')
  next()
}, function (req, res) {
  res.send('Hello from D!')
})

Response methods

下表中的methods可以给客户端发送一个res回应

app.route()

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')
  })

expreee.Router

通过express的Router()类创建一个模块或者可挂载的route处理程序。一个Router实例可以被看作是一个完整的中间件或者路由系统,

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

然后在其他文件中可以引入路由模块:
var birds = require(’./birds’)

// …

app.use(’/birds’, birds);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值