curl-Command返回html而不是node.js

我正在尝试使用以下Facebook-messenger-tutorial通过https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup通过heroku创建并托管一个Webhook。 因此,我目前正在测试对本地主机的cURL请求。此请求:

curl -H "Content-Type: application/json" -X POST "localhost:3000/" -d "{""object"": ""page"",""entry"": [{""messaging"": [{""message"": ""TEST_MESSAGE""}]}]}"

应该在cmd中返回EVENT RECEIVED,在第二个cmd窗口中返回TEST_MESSAGE,我将在其中执行我的应用程序。 为了执行该应用程序,我使用了$ node index.js,并且接收到“ webhook侦听”,这对我来说意味着我执行它的方式没有错误。

这是我的文件index.js的样子:

'use strict';

// Imports dependencies and set up http server
const
  express = require('express'),bodyParser = require('body-parser'),app = express().use(bodyParser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 3000,() => console.log('webhook is listening'));
// Creates the endpoint for our webhook 
app.post('/webhook',(req,res) => {  

  let body = req.body;

  // Checks this is an event from a page subscription
  if (body.object === 'page') {

    // Iterates over each entry - there may be multiple if batched
    body.entry.forEach(function(entry) {

      // Gets the message. entry.messaging is an array,but 
      // will only ever contain one message,so we get index 0
      let webhook_event = entry.messaging[0];
      console.log(webhook_event);
    });

    // Returns a '200 OK' response to all requests
    res.status(200).send('EVENT_RECEIVED');
  } else {
    // Returns a '404 Not Found' if event is not from a page subscription
    res.sendStatus(404);
  }

});
// Adds support for GET requests to our webhook
app.get('/webhook',res) => {

  // Your verify token. Should be a random string.
  let VERIFY_TOKEN = "somepassword"

  // Parse the query params
  let mode = req.query['hub.mode'];
  let token = req.query['hub.verify_token'];
  let challenge = req.query['hub.challenge'];

  // Checks if a token and mode is in the query string of the request
  if (mode && token) {

    // Checks the mode and token sent is correct
    if (mode === 'subscribe' && token === VERIFY_TOKEN) {

      // Responds with the challenge token from the request
      console.log('WEBHOOK_VERIFIED');
      res.status(200).send(challenge);

    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);      
    }
  }
});

这是我得到的响应(一些html代码):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>

我没有收到错误,只是返回了“错误”。

有人知道如何解决此问题并收到“ EVENT RECEIVED”和“ TEST_MESSAGE”吗?

注意:我看过以下文章Curl command no returnPrometheus API returning HTML instead of JSON,并且确实更改了与Facebook messenger bot error Unexpected token ' in JSON at position 0相关的本教程中的某些代码,但仍然无法修复这个。

我也读过How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?,但它也不能解决我的问题。

xiwangvsqiji 回答:curl-Command返回html而不是node.js

您忘记在curl命令中使用的网址中包含/ webhook

curl -H "Content-Type: application/json" -X POST "localhost:3000/webhook"
本文链接:https://www.f2er.com/3070947.html

大家都在问