【发布时间】:2020-07-30 00:22:11
【问题描述】:
我正在从 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 上找到了这个 proper-way-to-return-json-using-node-or-express,并查看了 Slack 和 Node.js 文档。我的应用程序已经进步了很多,但响应仍然没有正确呈现,所以我想我会在这里问。
有什么想法吗?
【问题讨论】:
-
使用适当的SDK