日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Express 入门之Router - worldtree_keeper的专栏 - CSDN博客

發(fā)布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Express 入门之Router - worldtree_keeper的专栏 - CSDN博客 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

要了解Router我們需要先知道到Application,首先,每一個express實例本身內(nèi)部就內(nèi)建了router,所以我們先從簡單的下手,先使用application;另外這里我們只選擇get方法,作為我們Router.Method, 之所以使用get是因為它足夠簡單;

?

精確匹配形式

1. get有很多種用法

?

?
  • var express = require("express");

  • var app = express();

  • app.get("/example/c",function(req, res){

  • res.send("Your url is /example/c");

  • });

  • app.listen(3000);

  • ?

    ?

    2. 參數(shù)形式

    ?

    ?

    ?
  • var express = require("express");

  • var app = express();

  • app.get('/user/:userid',function(req, res){

  • res.send(req.params.userid);

  • });

  • app.listen(3000,function(){console.log("server is listening on port 3000")});

  • ?

    參數(shù)形式另外的高級應用

    ?

    3. 正則表達式的形式

    ?

    ?
  • var express = require('express');

  • var app = express();

  • app.get(/example/,function(req, res){

  • res.send('it is use regular expression');

  • });

  • app.listen(3000);

  • ?

    ?

    ?

    4.? 神秘的參數(shù) next

    ?

    ?

    ?
  • var express = require('express');

  • var app = express();

  • var func1 = function(req, res, next){

  • console.log("this is first func");

  • next();

  • }

  • var func2 = function(req, res, next){

  • //不能這么寫,因為這樣就相當于后面有設置了一遍head

  • //res.send("this second func");

  • console.log("this is second func");

  • next();

  • }

  • var func3 = function(reg,res){

  • console.log("this is thrid func");

  • res.send("this is in the func3, end");

  • }

  • ?
  • //使用方式1

  • app.get("/example/",func1,func2,func3);

  • ?
  • //使用方式2

  • app.get("/example/",[func1,func2,func3]);

  • ?

    匹配好之后就是res

    res也有很多種形式

    比如jsonp

    ?

    ?
  • var express = require('express');

  • var app = express();

  • app.get('/example/defaultCallback?callback=foo',function(req, res){

  • res.jsop({"message":"this is default callback"});

  • });

  • app.get('/example/customizeCallback?cb=foo2',function(req, res){

  • app.set("jsonp callback name", 'cb');

  • res.jsonp({"message":"this is customize callback"});

  • })

  • app.listen(3000);

  • 默認情況下:http://localhost:3000/example/b?callback=foo

    指定情況下:http://localhost:3000/example/c?cb=foo2

    ?

    比如redirect

    ?
  • app.get("/example/d", function(req, res) {

  • var ua = req.get("user-agent");

  • if (!!ua && ua.toLowerCase().match(/android|ipad|iphone|ipod/)) {

  • console.log("this is mb");

  • res.redirect("http://m.browser.baidu.com/mb");

  • } else {

  • console.log("This is pc");

  • res.redirect("http://m.browser.baidu.com/pc");

  • }

  • });

  • ?

    http://localhost:3000/example/d

    在chrome里面,打開開發(fā)者模式,切換模擬器。


    ?


    參數(shù)形式高級應用

    ?

    ?
  • var express = require('express');

  • var app = express();

  • app.get('/user/:userage/:userid', function(req, res, next) {

  • console.log("in get method: userid:", req.params.userid);

  • console.log("in get method: userage:", req.params.userage);

  • next();

  • });

  • ?
  • ?
  • app.param("userage", function(req, res, next, value, key) {

  • console.log("in param key:", key);

  • console.log("in param value:", value);

  • next();

  • });

  • ?
  • ?
  • app.param("userid", function(req, res, next, value, key) {

  • console.log("in param key:", key);

  • console.log("in param value:", value);

  • next();

  • });

  • ?
  • ?
  • app.get('/user/:userid/:userage', function(req, res, next) {

  • res.send("userid and userage are:", req.params.userid, req.params.userage);

  • });

  • app.listen(3000);

  • ?

    ?

    前后順序與app.param的順序無關,只與

    app.get('/user/:userage/:userid', function(req, res, next) 的順序有關。

    ?

    OK,Application基本學習完畢了,我們就來說一下Router,其實沒有什么大不同,基本上是一致的。

    Router有幾種參數(shù)形式:

    1. function(func){}

    2. function(url, function){}

    第1種主要是針對于這個路由下的所有情況,都會使用經(jīng)過這個回調(diào)函數(shù)的處理。

    第2種情況是針對于這個路由下的指定的地址,才會觸發(fā)回調(diào)函數(shù)的處理。

    以下上干貨:

    ?

    ?
  • var express = require("express");

  • var app = express();

  • var router = express.Router();

  • router.use(function(req, res, next) {

  • console.log('%s %s %s', req.method, req.url, req.path);

  • next();

  • });

  • ?
  • ?
  • router.use(express.static(__dirname + "/bar"), function(req, res, next) {

  • next();

  • });

  • ?
  • ?
  • router.use(function(req, res) {

  • res.send("Hello world");

  • });

  • ?
  • ?
  • app.use('/foo', router);

  • ?
  • ?
  • app.listen(3000);

  • ?

    ?

    因為在app.use中已經(jīng)制定了父級目錄的地址,所以router只需要針對自己目錄進行處理即可。

    ?

    ?

    ?

    ?

    本文涉及的學習資源來源于

    http://expressjs.com/en/4x/api.html#router.METHOD

    總結

    以上是生活随笔為你收集整理的Express 入门之Router - worldtree_keeper的专栏 - CSDN博客的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。