node.js - NodeJS : outputting a string using Express EJS Template

标签 node.js express

我有一个代码,在出错时我会返回带有错误消息的状态:

if (err){
        res.render('page1', {status:2, msg:'Server Error : Probable unable to connect'});
        return;
}

在我的 page1.ejs 中,我按以下方式编码:

    var status = <%= status %>;
    alert ( 'The status = ' + status);
    if( status == 2)
    {
            var msg = "'" + <%= msg %> + "'";
            alert(msg);
    }

或者

    if( status == 2)
    {
            alert('"' + <%= msg %> + "'");
    }

我正在尝试获取状态和消息的值 - 但由于“消息”作为多个单词存在,我无法找到一种方法来捕获从 NodeJS 传递的完整字符串。 Firebug 显示错误为:

语法错误:参数列表后缺少 )

alert('"' + Server Error : Probable unable to connect+ "'");
-------------------|

最佳答案

试试这个:

alert('<%= msg %>');

关于node.js - NodeJS : outputting a string using Express EJS Template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229556/

相关文章:

node.js - 用动态 url 包裹标题

mysql - Mysql ExpressJS NodeJS 的多重验证

node.js - MongoDB聚合: any value of $exists

javascript - 如何使用 2 个不同的查询组合选项指定 1 个 api 路由

javascript - Node npm - 在前端引用 Three.js 模块

node.js - 如何使用restify上传文件

node.js - 连接到 Azure 容器时出错

node.js - 使用中间件在expressjs中的应用程序重定向

javascript - 如何在 Angular 使用 Electron cookies

mysql - 错误: ER_BAD_FIELD_ERROR: Unknown column 'claimSystemId' in 'field list'