res.send发送整个对象作为响应而不是内容

时间:2017-02-03 05:30:45

标签: node.js request response aws-lambda facebook-chatbot

  'use strict'
    var express = require('express');
    var bodyParser = require('body-parser');
    var request = require('request');
    var awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
    var app = express();
    var port = process.env.PORT || 5000;
    var VALIDATION_TOKEN = 'Serverless_My_Token';
    app.set('port', (port));
// Process application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({extended: true}));
app.use(awsServerlessExpressMiddleware.eventContext());
    // Process application/json
    app.use(bodyParser.json());
// for Facebook verification
        app.get('/webhook/', function (req, res) {
            var jsonObject = JSON.parse(req.rawHeaders[1]);
            if (jsonObject.params.querystring['hub.verify_token'] === 'serverless_my_token') {
         var hubChallenge = jsonObject.params.querystring['hub.challenge'];

         return res.send(hubChallenge);
    }
    res.send('Error, wrong token')
        });

调用get API时,res.send正在发送整个响应对象,

{
  "statusCode": 200,
  "body": "",
  "headers": {
  },
  "isBase64Encoded": false
}

我只需要发送hubChallenge变量作为响应:

hub.challenge number:例如:245421998作为回复。

如何仅将hubchallenge变量作为响应而不是包含状态的整个对象返回。等。

错误:

The URL couldn't be validated. Response does not match challenge, expected value="245421998", received="{\"statusCode\":200,..."

1 个答案:

答案 0 :(得分:0)

不应该在app = express();之后的某处将其添加到您的代码中:

app.use(awsServerlessExpressMiddleware.eventContext());

现在它导致您导入awsServerlessExpressMiddleware,但您没有在任何地方使用它。