首页 > 解决方案 > 节点应用程序发布方法console.log中的bodyParser响应“未定义”

问题描述

我正在尝试使用中间件来捕获发布请求的主体,并动态使用 telnyx API 端点的消息 ID 来填充其他字段等。

多次遵循此处的文档,当我向我的 telnyx 号码发送消息时,无法让终端输出除未定义之外的任何内容:https ://developers.telnyx.com/docs/v2/messaging/quickstarts/receiving-短信和彩信

我尝试过 cors 认为这可能是我在其他帖子中读到的问题,并且知道 express 和 bodyParser 不再捆绑在一起,所以我也使用了所有这些来尝试捕获数据。如果很明显,请原谅我。

require('dotenv').config();

const axios = require('axios').default;
const cors = require('cors');
const telnyx = require("telnyx")("MYAPIKEY")

const express = require('express');

const app = express();

app.use(express.urlencoded( {extended: true} ));
app.use(express.json());

app.post('/webhook', (req, res) => {
    console.log(`New message from ${req.body.from}: ${req.body.body}`);
});

const port = 8080;
app.listen(port, () => console.log(`App running on port ${port}`));

以下是依赖项:

{
  "name": "telnyxjs",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "telnyx": "^1.13.1"
  }
}

终端输出 ngrok 捕获 post 数据

标签: node.jsexpressbody-parser

解决方案


推荐阅读