Express.js – app.set() 方法
app.set() 函数将设置名称分配或设置为值。这可以按照用户需要的方式存储任何类型的变量,但是有一些特定的名称可以用来配置服务器的行为
可以使用 set 功能配置的一些属性为 −
env
etag
jsonp escape 等
语法
app.set(name, value)
示例 1
创建一个名为 "appSet.js" 的文件,并复制以下代码段。创建文件后,使用命令 "node appSet.js" 来运行此代码。
// app.set() Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Setting the value to name
app.set('title', 'Welcome to TutorialsPoint');
// Creating an endpoint
app.get('/', (req, res) => {
res.send(app.get('title'));
console.log(app.get('title'));
})
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});现在,点击以下端点/api 以调用函数
GET https://:3000/
输出
C:\home
ode>> node appSet.js Server listening on PORT 3000 Welcome to TutorialsPoint
示例 2
我们来看另一个示例。
// app.set() Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
var PORT = 3000;
// Setting the value to name
app.set('title', 'Hi, The requested page is not available');
// Creating an endpoint
app.get('/*', (req, res) => {
res.send(app.get('title'));
console.log(app.get('title'));
})
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});对于上面的函数,你可以在 '/' 后面调用任何端点,它只会通过上面的函数传递。例如
https://:3000/,
输出
C:\home
ode>> node appRoute.js Server listening on PORT 3000 Hi, The requested page is not available
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP