gpt4 book ai didi

node.js - GraphQL - POST 正文丢失。你忘记使用 body-parser 中间件了吗?

转载 作者:行者123 更新时间:2023-12-04 01:36:54 24 4
gpt4 key购买 nike

我的 graphql 上不断出现以下错误查询,不知道为什么:
POST body missing. Did you forget use body-parser middleware?
我在这里做一些奇怪的事情吗?我在网上尝试了使用 body-parser 的不同建议,但似乎仍然无法修复它。

服务器:

require('babel-polyfill')

const express = require('express')
const router = require('./middleware')
const expressStaticGzip = require('express-static-gzip')
const app = express()
const port = process.env.EXPRESS_PORT || 4000
const bodyParser = require('body-parser')

app.use(/\/((?!graphql).)*/, bodyParser.urlencoded({ extended: true }))
app.use(/\/((?!graphql).)*/, bodyParser.json())
app.use('/search/data', expressStaticGzip('public'))
app.use('/', router)

app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})

路由器

const router = express.Router()
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
const { authorization = '' } = req.headers
const universalFetch = (url, opts = {}) => {
return fetch(url, {
...opts,
headers: {
...opts.headers,
authorization,
},
})
}
const request = createRpcClient(universalFetch)

const methods = {}

const catalog = Object.keys(methods).reduce((catalog, method) => {
catalog[method] = params => request(methods[method], params)
return catalog
}, {})
return { catalog, fetch: universalFetch }
},
})

router.use(bodyParser.json())
router.use(bodyParser.text({ type: 'application/graphql' }))
router.use('*', renderer)
server.applyMiddleware({ app: router })

最佳答案

在我的特殊情况下,客户端只是错过了带有“application/json”值的“Content-type” header 。添加后,错误消息已消失。

关于node.js - GraphQL - POST 正文丢失。你忘记使用 body-parser 中间件了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com