Express.js 中的 req.route 屬性
req.route 屬性包含最近匹配的路由的字串格式。
語法
req.route
示例 1
建立一個名為 “reqRoute.js” 的檔案並複製以下程式碼片段。建立檔案後,使用命令 “node reqRoute.js” 執行此程式碼,如下例所示 −
// req.route Property Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint and checking req.route
app.get('/api', function (req, res) {
console.log(req.route);
res.send();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 請求訪問以下端點:localhost:3000/api
輸出
C:\home
ode>> node reqRoute.js Server listening on PORT 3000 Route { path: '/api', stack: [ Layer { handle: [Function], name: '', params: undefined, path: undefined, keys: [], regexp: /^\/?$/i, method: 'get' } ], methods: { get: true } }
示例 2
我們來看另一個示例。
// req.route Property Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint and checking req.route
app.get('/api', function (req, res) {
console.log("Path is: " + req.route.path);
res.send("Path is: "+ req.route.path);
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
var express = require('express');
var app = express();
var PORT = 3000;使用 GET 請求訪問以下端點:localhost:3000/api
輸出
C:\home
ode>> node reqRoute.js Server listening on PORT 3000 Path is: /api
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP