如果不使用模板引擎,我们会采取拼接的方式返回页面:
router.get('/login', function(req, res, next) {
res.set('Content-Type','text/html');
const data = new Date();
const html = `
<html>
<head>
<title>Blog</title>
</head>
<body>
<h1>login ande now: ${data}</h1>
</body>
</html>
`
return res.send(html)
});
代码来自:链接
如果使用了模板引擎,我们可以单独渲染页面:
// 创建登录页面路由(login省略了.html)
router.get('/login',function (req, res) {
res.render('admin/login')
});
所以使用模板引擎的好处就不言而喻了。