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