var express = require('express'); var app = express(); app.get("*", function(req, res) { console.log(req.path); res.send("req.host獲取主機名,req.path獲取請求路徑名!"); }); app.listen(80);
query是一個可獲取客戶端get請求路徑參數的對象屬性,包含着被解析過的請求參數對象,默認爲{}。html
var express = require('express'); var app = express(); app.get("*", function(req, res) { console.log(req.query.參數名); res.send("測試query屬性!"); }); app.listen(80);
經過req.query獲取get請求路徑的對象參數值。express
格式:req.query.參數名;請求路徑以下示例:
api/search?username=Lenka&company=alibaba
req.query.username // "Lenka"
req.query.company // "alibaba"api
和屬性query同樣,經過req.param咱們也能夠獲取被解析過的請求參數對象的值。瀏覽器
格式:req.param("參數名");請求路徑以下示例:緩存
例1: 獲取請求根路徑的參數值,如/?n=Lenka,方法以下:app
var express = require('express'); var app = express(); app.get("/", function(req, res) { console.log(req.param("n")); //Lenka res.send("使用req.param屬性獲取請求根路徑的參數對象值!"); }); app.listen(80); express deprecated req.param(name): Use req.params, req.body, or req.query instead
例2:咱們也能夠獲取具備相應路由規則的請求對象,假設路由規則爲 /user/:name/,請求路徑/user/mike,以下:ide
app.get("/user/:name/", function(req, res) { console.log(req.param("name")); //mike res.send("使用req.param屬性獲取具備路由規則的參數對象值!"); });
PS:所謂「路由」,就是指爲不一樣的訪問路徑,指定不一樣的處理方法。測試
看了上面的示例,試一試使用req.param屬性解析一個請求路徑對象,並獲取請求參數值。ui
和param類似,但params是一個能夠解析包含着有複雜命名路由規則的請求對象的屬性。this
格式:req.params.參數名;
例1. 如上課時請求根路徑的例子,咱們就能夠這樣獲取,以下:
var express = require('express'); var app = express(); app.get("/user/:name/", function(req, res) { console.log(req.params.name); //mike res.send("使用req.params屬性獲取具備路由規則的參數對象值!"); }); app.listen(80);
查看運行結果,和param屬性功能是同樣的,一樣獲取name參數值。
例2:固然咱們也能夠請求複雜的路由規則,如/user/:name/:id,假設請求地址爲:/user/mike/123,以下:
app.get("/user/:name/:id", function(req, res) { console.log(req.params.id); //"123" res.send("使用req.params屬性複雜路由規則的參數對象值!"); });
對於請求地址具備路由規則的路徑來講,屬性params比param屬性是否是又強大了那麼一點點呢!
http://www.expressjs.com.cn/4...
For example, when :user is present in a route path, you may map user loading logic to automatically provide req.user to the route, or perform validations on the parameter input.
router.param('user', function(req, res, next, id) { // try to get the user details from the User model and attach it to the request object User.find(id, function(err, user) { if (err) { next(err); } else if (user) { req.user = user; next(); } else { next(new Error('failed to load user')); } }); });
A param callback will be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples.
router.param('id', function (req, res, next, id) { console.log('CALLED ONLY ONCE'); next(); }) app.get('/user/:id', function (req, res, next) { console.log('although this matches'); next(); }); app.get('/user/:id', function (req, res) { console.log('and this matches too'); res.end(); });
On GET /user/42, the following is printed:
CALLED ONLY ONCE although this matches and this matches too
router.get('/:uid', controller.get.findByUid); router.get('/', controller.findTPList); router.put('/:uid', controller.edit); router.put('/:uid/status',controller.changeState);
api/123456
send()方法向瀏覽器發送一個響應信息,並能夠智能處理不一樣類型的數據。格式以下:res.send([body|status], [body]);1.當參數爲一個String時,Content-Type默認設置爲"text/html"。res.send('Hello World'); //Hello World2.當參數爲Array或Object時,Express會返回一個JSON。res.send({ user: 'tobi' }); //{"user":"tobi"}res.send([1,2,3]); //[1,2,3]3.當參數爲一個Number時,而且沒有上面提到的任何一條在響應體裏,Express會幫你設置一個響應體,好比:200會返回字符"OK"。res.send(200); // OKres.send(404); // Not Foundres.send(500); // Internal Server Errorsend方法在輸出響應時會自動進行一些設置,好比HEAD信息、HTTP緩存支持等等。