curl 命令返回 html 而不是 node.js

curl-Command returning html instead of node.js

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

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

应该 return 在 cmd 中收到事件,并且 TEST_MESSAGE 在第二个 cmd-window 中,我正在执行我的应用程序。 为了执行应用程序,我使用了 $ node index.js,并且我收到了 "webhook listening",这对我来说意味着我执行它的方式没有错误。

这是我的文件 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', (req, 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>

我没有收到错误,只是 "wrong" return.

有没有人知道如何解决这个问题并接收 "EVENT RECEIVED" 和 "TEST_MESSAGE"?

注意:我已经看过以下文章,, Prometheus API returning HTML instead of JSON and I did change something of the code from the tutorial relating to 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?,但也没有解决我的问题。

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

curl -H "Content-Type: application/json" -X POST "localhost:3000/webhook"