Express.js – req.secure 属性
req.secure 属性返回一个布尔值,如果建立了 TLS 连接,则返回真;否则,将返回假。
其逻辑类似于以下方法 −
--> req.protocol == "https"
语法
req.secure
示例 1
创建一个名为 "reqSecure.js" 的文件,并复制以下代码段。创建文件后,使用命令 "node reqSecure.js" 运行此代码,如下例所示 −
// req.secure Property Demo Example
// Importing the express
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
app.get('/api', function (req, res) {
console.log(req.secure);
res.end(req.secure);
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 请求访问以下端点:https://:3000/api
输出
C:\home
ode>> node reqSecure.js Server listening on PORT 3000 false
示例 2
我们再看一个示例。
// req.secure Property Demo Example
// Importing the express
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
app.get('/api', function (req, res) {
if (req.secure) {
console.log("Secured Connection")
res.end();
} else {
console.log("Connection is Not Secured");
res.end();
}
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 请求访问以下端点 − https://:3000/api
输出
C:\home
ode>> node reqSecure.js Server listening on PORT 3000 Connection is Not Secured
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP