javascript - Node 快速返回带有链接的消息

标签 javascript node.js express

我有一个 Node 程序,如果出现错误,需要发送错误消息, 这可行,但是我想在消息中添加一个链接。

类似的东西

Something went wrong and we cannot open your application.
Follow this tutorial to proceed.

本教程将是一个链接,例如https://nodejs.org/en/about/

我怎样才能做到这一点?

...
    } catch (e) {
        throw new Error('Something went wrong and we cannot open your application.Follow <a href="https://nodejs.org/en/about/">this</a> tutorial to proceed.'
        )
    }

我通过像res.send这样的 express 发送数据

app.get('/', async function (req, res) {

    try {
        const url = await invokeApp()

    } catch (e) {
        res.send(e.message)
    }

})

更新

在尝试下面的建议时,我能够获取链接,但消息顺序已更改,知道如何修复它吗?

    } catch (e) {
        throw new Error('Something went wrong and we cannot open your application.Follow <a href="https://nodejs.org/en/about/">this</a> tutorial to proceed.'
        )
    }

enter image description here

最佳答案

试试这个:

...
    } catch (e) {
        throw new Error('Something went wrong and we cannot open your application. Follow <a href="https://nodejs.org/en/about/">this</a> tutorial to proceed.')
    }

您可以使用res.send带有 html 字符串——即 res.send('<p> html </p>') - 并在另一边处理。

关于javascript - Node 快速返回带有链接的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63136136/

相关文章:

javascript - 动态改变div的位置

javascript - Karma - 如果我不使用 PhantomJS 浏览器,则不会运行测试

javascript - Node.js 和 expressjs 路由到另一个文件夹

node.js - 在 Node/Express 服务器中使用 async/await 是个坏主意吗?

javascript - 在 jquery 中上下移动元素

javascript - 从 Servlet 到 Jquery 的数组的数组

node.js - 有什么方法可以在使用await时获得4xx响应正文而不尝试...catch?

node.js - Typescript 在 Mongoose pre hook 中抛出编译错误(预期有 1 个参数,但得到了 0)

javascript - Mocha 测试由 bluebird 驱动的 Node 样式回调

AngularJS:数组表示法中的 POST 参数