Node.js: SyntaxError: Unexpected token : while compiling ejs

Node.js: SyntaxError: Unexpected token : while compiling ejs

我正在尝试使用 Node.JS 将过滤器应用于 EJS,但出现以下错误。我希望消息内容以大写形式显示。

D:\Apps\Templating\ejs>node server.js
D:\Apps\node_modules\ejs\lib\ejs.js:470
      throw e;
            ^
SyntaxError: Unexpected token : while compiling ejs
    at Function (native)
    at Object.Template.compile (D:\Apps\node_m
odules\ejs\lib\ejs.js:460:12)
    at Object.compile (D:\Apps\node_modules\ej
s\lib\ejs.js:288:16)
    at handleCache (D:\Apps\node_modules\ejs\l
ib\ejs.js:147:16)
    at Object.exports.render (D:\Apps\node_mod
ules\ejs\lib\ejs.js:315:10)
    at Object.<anonymous> (D:\Apps\Templating\
ejs\server.js:3:17)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

这是我的代码- server.js:

var ejs = require('ejs');
var template = "<%=: message | upcase %>";
console.log(ejs.render(template,{message : 'hello ejs with filter'}));

我已经使用包管理器安装了 ejs,它适用于没有过滤器 (:) 的普通脚本。下载的EJS版本是最新的-2.3.3,节点版本是0.12.4.

npm install ejs

如有任何帮助,我们将不胜感激。提前致谢。

我现在知道了。 EJS 从第 2 版开始进行了一些更改,包括删除过滤器功能。 https://github.com/tj/ejs

当前版本ejs (v2.3.3) 不支持过滤器,过滤器已从版本 2 中删除。如果要使用过滤器,需要安装 1.x 版本:

$ npm i ejs@1.x

在 ejs v2 中,如果有正常代码或函数但没有对象,则必须在代码中不带 = 或 - 如果你有一个对象,你必须以 = 或 - 开头 符合 Express 视图系统 中间静态缓存 JavaScript 条件等的无缓冲代码 <% code %> 默认使用 <%= code %> 转义 html 使用 <%- code %> 的未转义缓冲 支持标签自定义 过滤器支持设计人员友好的模板 包括 客户端支持 换行符 <% code -%> or <% -%> or <%= code -%> or <%- code -%> 在此处查看更多信息: https://github.com/tj/ejs