Express.js – app.engine() 方法
app.engine() 方法用於將給定的模板引擎回撥註冊為“ext”。根據預設設定,require() 方法需要基於該函式的引擎。
對於未提供副檔名(或想對映不同副檔名)的引擎或直接表達,請使用以下方法。
app.engine('html', require('ejs').renderFile)語法
app.engine(ext, callback)
示例 1
建立一個名為“appEngine.js”的檔案,並複製以下程式碼段。建立檔案後,使用命令“node appEngine.js”執行此程式碼。
// app.engine() Method Demo Example
// Importing the express module
const express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Setting the html page from view
app.engine('html', require('ejs').renderFile);
// Defining an endpoint to retrieve html page
app.get('/api', function (req, res) {
res.render("api.html")
});
// App listening on the below port
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});api.html
<html> <head> <title>app.engine() Demo Example</title> </head> <body> <h2>Welcome to Tutorials Point</h2> </body> </html>
現在,在瀏覽器中輸入以下端點
https://:3000/api
輸出
C:\home
ode>> node appEngine.js Server listening on PORT 3000
在瀏覽器中,你將看到以下螢幕

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP