req.body 在 Node.js 中未定义,即使当我记录 req 时它显示了主体及其内容

晚上好,我正在尝试将 req.body 记录到控制台,但它说它是未定义的,即使当我执行 console.log(req) 时,主体及其内容都会显示出来。我正在尝试制作 Twilio 文本应用程序。我迷失了,我不确定它是否说身体未定义,即使它已定义。任何指导表示赞赏。

/* Nodejs index.js */

const express = require('express')

const app = express()

const port = 3000

const bodyParser = require('body-parser');



const indexRouter = require('./routes/index-router');



/* View Engine */

const ejs = require('ejs');

app.set('view engine', 'ejs');



app.use('/', indexRouter);





app.use(express.static('public'));

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

app.use(bodyParser.json());



app.get('*', (req, res) => {

    res.sendStatus(404);

})



app.listen(port, () => {

    console.log(`Twilio app listening at http://localhost:${port}`)

})


/* Index Router */

var express = require('express');

var router = express.Router();

var path = require('path');

const bodyParser = require('body-parser');


router.use(bodyParser.json());



require('dotenv').config();


var accountSid = process.env.ACCOUNT_SID; // Your Account SID from www.twilio.com/console

var authToken = process.env.AUTH_TOKEN;   // Your Auth Token from www.twilio.com/console


var twilio = require('twilio');

var client = new twilio(accountSid, authToken); 



/* GET home page. */

router.get('/', function (req, res, next) {

    res.render('index')  

});



  /*Twilio */

router.post('/', (res, req) => {

     /* client.messages.create({

        body: `${req.body.text}`,

        to: '+17185412931',  // Text this number

        from: '+12058946009' // From a valid Twilio number

    })

        .then((message) => console.log(message.sid)); */

        console.log(req);

}) 


module.exports = router;



/*  AJAX Request */







$('#text').on('submit', function (e) {

    e.preventDefault();


    const message = $("#message").val();



    const data = {

        message: message,

    };

});



慕婉清6462132
浏览 107回答 1
1回答

繁花不似锦

req/res 的位置错误。更改(res, req)为(req, res)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript