0

这个应用程序的想法是,无论何时调用任何 URL,它都应该给出 HTTP 响应。我正在尝试将数据发送到 HTTP 标头,但我不知道如何填充内容,也无法在任何地方找到解决方案。

这是我的代码

const express = require('express');

let helmet = require('helmet')


const app = express();

app.use(helmet());

app.use((req, response, next) => {
response.writeHead(200, {'Content-Type': 'application/json'});

const link = req.protocol + '://' + req.get('host') + req.originalUrl;
const data = [{ 'text' : 'Meeting Created',
                'attachment' : 
                    {'title':'Board Created', 
                        'text' : 'Visit this URL and share with your participants so that 
                            they can join.',
                        'title_link': link
                    }
                }];


const responseBody = {data};

response.end(JSON.stringify(responseBody))


 });

 const port = process.env.PORT || 4000;

 app.listen(port , () => {
   console.log('Port is listening');
 })

运行应用程序后..我在标题中得到以下结果

HTTP/1.1 200 OK X-DNS-Prefetch-Control: off X-Frame-Options: SAMEORIGIN Strict-Transport-Security: max-age=15552000; includeSubDomains X-Download-Options: noopen X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Content-Type: application/json Date: Thu, 30 Apr 2020 08:17:05 GMT Connection: keep-alive

但我想要 HTTP 标头指令中的其他数据。就像下面

期望的输出:

{
   "statusCode": 200,
   "content": "{\"text\":\"Meeting Created\",\"attachments\":[{\"title\":\"Join 
    Meeting\",\"text\":\"Visit this URL and share with your meeting participants so that 
    they can join: https://mnow.io/800244\",\"title_link\":\"https://mnow.io/800244\"}]}",
   "headers": {
    "x-dns-prefetch-control": "off",
    "x-frame-options": "SAMEORIGIN",
    "strict-transport-security": "max-age=15552000; includeSubDomains",
    "x-download-options": "noopen",
    "x-content-type-options": "nosniff",
    "x-xss-protection": "1; mode=block",
    "access-control-allow-origin": "*",
    "content-type": "application/json; charset=utf-8",
    "content-length": "216",
    "etag": "W/\"d8-KMGsyQ3ByyfI5IsF7yiwJ86lLWw\"",
    "date": "Tue, 28 Apr 2020 08:11:19 GMT",
    "connection": "close"
  },
  "data": {
  "text": "Meeting Created",
  "attachments": [
  {
    "title": "Join Meeting",
    "text": "Visit this URL and share with your meeting participants so that they can join: 
     http://localhost:4000/",
    "title_link": "http://localhost:4000/"
  }
]
}
}
4

2 回答 2

0

您可以使用 express 的response.set函数在发送响应之前设置标头。

此外,如果您的响应是 JSON,那么您不需要显式设置状态代码或内容类型。一个简单的res.send调用就可以了。Express 将负责休息。

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

app.use(helmet());


app.use((req, response) => {

    const link = req.protocol + '://' + req.get('host') + req.originalUrl;
    const data = [{
        'text': 'Meeting Created',
        'attachment':
        {
            'title': 'Board Created',
            'text': 'Visit this URL and share with your participants so that they can join:' + link,
            'title_link': link
        }
    }];

    // You don't need to set content type for JSON response. The headers for that are set automatically by express.
    response.send({ data });
});


const port = process.env.PORT || 4000;

app.listen(port, () => {
    console.log('Port is listening');
})
于 2020-04-30T08:39:08.827 回答
0

通过使用 response.writeHead()。我得到了想要的输出。

const express = require('express');

    let helmet = require('helmet')


    const app = express();

    app.use(helmet());


    app.use((req, response, next) => {

        const link = req.protocol + '://' + req.get('host') + req.originalUrl;
        const data = [{ 'text' : 'Meeting Created',
                        'attachment' : 
                            {'title':'Board Created', 
                                'text' : 'Visit this URL and share with your participants so that they can join:'+ link,
                                'title_link': link
                            }
                        }];

        const responseBody = {data};


        response.writeHead(200, {'Content-Type': 'application/json; charset=utf-8','data' : JSON.stringify(data)}); // Added This line


        response.end(JSON.stringify(responseBody))


    });


    const port = process.env.PORT || 4000;

    app.listen(port , () => {
        console.log('Port is listening');
    })
于 2020-04-30T10:01:31.200 回答