del*_*rce 2 ejs node.js express
我希望以下控制器在响应时返回一个 html 字符串。目前正在对控制器中的字符串进行硬编码,我认为这不是正确的方法。
router.get('/', function(req, res) {
var employeeDetails; // JSON File Containing Details
// I need ejs to build the page using employeeDetails and store that as a
// string and return this string as the response
});
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您将模板作为字符串,则可以调用ejs.render(template).
您必须首先将模板文件作为字符串读取,因此您最终会执行以下操作:
import * as ejs from 'ejs';
import { readFile as _readFile } from 'fs';
import { promisify } from 'util';
const readFile = promisify(_readFile);
router.get('/', async function(req, res) {
const template = await readFile(/* your template */, 'utf-8');
const employeeDetails = await readFile(/* your json file */, 'utf-8');
const html = ejs.render(template, { /* your data */ });
// now you have your rendered html as a string
// and can e.g.:
res.send(html);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1669 次 |
| 最近记录: |