Express.js – app.set() 方法
该函数为值分配或设置设置名称。这可以根据用户的需要存储任何类型的值,但有一些特定的名称可用于配置服务器的行为app.set()
可以使用set功能配置的一些属性是-
环境
标签
jsonp转义等
语法
app.set(name, value)
示例1
创建一个名为“appSet.js”的文件并复制以下代码片段。创建文件后,使用命令“nodeappSet.js”运行此代码。
//app.set()演示示例
//导入express模块
var express = require('express');
//初始化express和端口号
var app = express();
var PORT = 3000;
//将值设置为名称
app.set('title', 'Welcome to nhooo');
//创建端点
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来调用函数
获取http://localhost:3000/
输出结果
C:\home\node>> node appSet.js Server listening on PORT 3000 Welcome to nhooo
示例2
让我们再看一个例子。
//app.set()演示示例
//导入express模块
var express = require('express');
//初始化express和端口号
var app = express();
var PORT = 3000;
//将值设置为名称
app.set('title', 'Hi, The requested page is not available');
//创建端点
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);
});对于上面的函数,你可以在'/'之后调用任何端点,它只会通过上面的函数传递。例如
http://localhost:3000/api,
http://localhost:3000/,
http://localhost:3000/path等
输出结果
C:\home\node>> node appRoute.js Server listening on PORT 3000 Hi, The requested page is not available
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短