首页 > 解决方案 > 为什么 Node.js 响应 JSON 在 Slack 中以原始形式显示?

问题描述

我正在从 Node.js 服务器向 Slack 应用程序发送 JSON 响应。此响应以原始格式(如 JSON)显示,而不是正确格式化。

复制问题的最少代码:
server.js:

const express = require('express');
const app = express();

// POST request processing
app.post('/', function(req, res){
    var arr1 = [{"type":"section","text":{"type":"mrkdwn","text":"*Lorem Ipsum*"}},
    {"type":"section","text":{"type":"mrkdwn","text":"_Lorem Ipsum_"}},
    {"type":"section","text":{"type":"mrkdwn","text":"`Lorem Ipsum`"}}]
    res.setHeader('Content-Type', 'application/json');
    res.json(arr1);  
});

// Listen to the AppEngine/Heroku - specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log('Server listening on port ${PORT}...');
});

JSON 消息应呈现为此处可见: api.slack.com/tools/block-kit-builder

在此处输入图像描述

相反,它显示如下:

在此处输入图像描述

我也尝试过这种方法和许多其他方法:

res.end(JSON.stringify(arr1));

我在 SO 上找到了这个正确的返回 json-using-node-or-express,并查看了 Slack 和 Node.js 文档。我的应用程序已经进步了很多,但响应仍然没有正确呈现,所以我想我会在这里问。

有任何想法吗?

标签: node.jsjsonslack

解决方案


请使用简单的快速回复res.send(your JSON object);


推荐阅读