如何在express中测试req.flash()?

发布于 2024-12-09 08:39:32 字数 387 浏览 5 评论 0原文

我目前有一个要测试的 Express/Node 应用程序,但它给出的响应格式为 req.flash('warn', 'message gets here');

不幸的是,文档express.js 上的内容并没有非常彻底地描述此消息如何传输到客户端。

我知道 expresso 有一个 assert.response() 函数来测试响应对象。我想知道闪存消息去哪里,以及如何以类似的方式测试它(或者如果不可能,我应该通过响应对象发送所有内容)。

I currently have an express/node application I want to test, and but responses that it gives are in the form of req.flash('warn', 'message goes here');

Unfortunately, the documentation on express.js does not describe how this message travels to the client very thoroughly.

I know expresso has an assert.response() function that tests response objects. I was wondering where the flash message goes, and how I can test it in a similar way (or if it's not possible, and I should be sending everything through the response object).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-12-16 08:39:32

我不认为这应该是这样的。您不能仅使用 req.flash() 作为响应请求的唯一方式。

它更像是一种在普通模板上向用户闪现消息的简单方法 - 例如,在插入/创建文章后,您可以说:

req.flash('error', 'could not insert because .... ');

或者

req.flash('info', 'article added successfully!');

对于我的上一个项目,我向我的应用程序添加了两个动态助手:

app.dynamicHelpers({
    info: function (req, res) {
        return req.flash('info');
    },
    error: function (req, res) {
        return req.flash('error');
    }
});

这样我就可以在我看来这样说:

<% if (info !== undefined && info != "") { %>
<div class="infoBubble"> 
    <%= info %>
</div>
<% } %>

<% if (error !== undefined && error != "") { %>
<div class="errorBubble"> 
    <strong>Fehler</strong>: <%= error %>
</div>
<% } %>

结果如下所示:

screenshot of flash bubble

i don't think this is supposed to work like that. you can't just use req.flash()as your only way to respond to a request.

it's more like an easy way to flash messages to the user on your normal templates - e.g. after inserting/creating an article you can either say:

req.flash('error', 'could not insert because .... ');

or

req.flash('info', 'article added successfully!');

for my last project i then added two dynamic helpers to my app:

app.dynamicHelpers({
    info: function (req, res) {
        return req.flash('info');
    },
    error: function (req, res) {
        return req.flash('error');
    }
});

so that i can just say sth like this in my view:

<% if (info !== undefined && info != "") { %>
<div class="infoBubble"> 
    <%= info %>
</div>
<% } %>

<% if (error !== undefined && error != "") { %>
<div class="errorBubble"> 
    <strong>Fehler</strong>: <%= error %>
</div>
<% } %>

result looks like this:

screenshot of flash bubble

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文