Express.js - app.locals 屬性
app.locals 物件定義應用程式內部作為區域性變數的屬性。一旦設定了 app.locals 屬性的值,它將在應用程式的整個生命週期內保持存在。res.locals 屬性僅在請求生命週期內有效。
語法
app.locals
示例 1
建立一個檔案 "appLocals.js",並複製以下程式碼片段。建立檔案後,使用 "node appLocals.js" 命令來執行此程式碼。
// app.locals code Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Setting the below email throught out the application
app.locals.email = 'hi@tutorialspoint.com'
console.log("Email Configured is: ", app.locals.email);輸出
C:\home
ode>> node appLocals.js Email Configured is: hi@tutorialspoint.com
示例 2
我們來看另一個示例。
// app.locals code Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Setting the multiple variables throughout the application
app.locals.domain = 'www.tutorialspoint.com'
app.locals.age = '30'
app.locals.company = 'Tutorials Point Ltd'
console.log(app.locals);輸出
C:\home
ode>> node appLocals.js [Object: null prototype] { settings: { 'x-powered-by': true, etag: 'weak', 'etag fn': [Function: generateETag], env: 'development', 'query parser': 'extended', 'query parser fn': [Function: parseExtendedQueryString], 'subdomain offset': 2, 'trust proxy': false, 'trust proxy fn': [Function: trustNone], view: [Function: View], views: '/home/mayankaggarwal/mysql-test/views', 'jsonp callback name': 'callback' }, domain: 'www.tutorialspoint.com', age: '30', company: 'Tutorials Point Ltd' }
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP