提问者:小点点

Node express返回带有链接的消息


我有一个节点程序,在发生错误的情况下,需要发送错误消息,这是工作的,但我想添加到消息也一个链接。

类似于

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.'
        )
    }

然后我通过express发送数据,如res.send

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.'
        )
    }


共2个答案

匿名用户

请尝试以下操作:

...
    } 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('

html

')-并在另一端处理。

匿名用户

试试这个

res.json({ err : e.message , link : "error/url"})

如果出现错误,您可以从这个对象访问url,并在前端显示链接。