node.js - ExpressJs 请求正文为空
问题描述
我有一个快速应用程序localhost:5000
和一个反应应用程序localhost:3000
。
我通过调用它
fetch(`${backendUrl}/charge`, {
method: "POST",
mode: "no-cors",
headers: {
"Content-Type": "application/json"
},
body: {
stripeToken: token,
chargeAmount: this.state.donationAmount
}
})
并回应
function route(req, res) {
console.log(req.body);
}
服务器应正确配置为使用 CORS,但正文仍然是空的。
//configure env variables
require("dotenv").config();
//import packages
var express = require("express");
var bodyParser = require("body-parser");
var cors = require("cors");
//import route functions
const StripeRoute = require("./StripeRoute");
//setup app
const app = express();
const port = process.env.PORT || 5000;
//setup bodyparser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Setup CORS
app.use(cors());
app.options("*", cors()); // include before other routes
//Connect functions to API routes
app.post("/charge", StripeRoute);
module.exports = app;
解决方案
推荐阅读
- view - 初始化 Prism 应用程序期间的 DataGrid AutoGeneratingColumn 问题
- javascript - Wsy 这个箭头函数不适用于“arguments.length”吗?
- dax - SUMX DISTINCT PowerPivot 未按预期工作
- javascript - 只有在打字稿中为真时,我才能向对象添加属性吗?
- python - Pandas 中的动态过滤/屏蔽
- vba - MSXML2 - 如何搜索特定节点并替换其子节点
- node.js - 使用流星 restivus 上传多部分文件
- python - 如何在不复制粘贴的情况下复制一个函数以创建另一个函数并进行一些更改
- pic - pic18F47J53 RTCC 未启动
- flutter - 弹出页面后,再点击一次按钮