我正在尝试在 Postman 发送的正文中捕获原始数据。这是原始数据:
{
"hello": "world"
}
我在服务器中使用 app.use(express.json())
。当我发送 post 请求时,我只得到一个空的 JSON。为什么会发生这种情况?
App.js 代码:
import express from "express"
import { connectDb } from "./connectDb.js"
import create from "./routes/create.js" // router
const app = express()
app.use(express.json())
connectDb()
const PORT = process.env.PORT || 5000
app.use("/api", create)
app.listen(PORT, () => console.log(PORT, "Connected..."))
路由器代码:
import express from "express"
import Game from "../models/gamesModel.js" // mongoose model
const router = express.Router()
router.route("/create/game").post(async (req, res) => {
console.log(req.body)
try {
const game = await Game.create(req.body)
res.json(game)
} catch (error) {
res.json({ message: "Invalid Information" })
}
})