如何获取从axios发布请求传递到nuxt serverMiddleware的数据?

时间:2020-07-12 20:48:11

标签: vue.js axios nuxt.js

我正在尝试使用axios将一些数据传递到serverMiddleware,但似乎无法在serverMiddleware中获取数据

guilds页面脚本:

<script>
export default {
  async fetch() {
    const token = 'testToken'
    const guilds = await this.$axios.$post('/api/guilds', { token })
  }
}
</script>

serverMiddleware:

import axios from 'axios'
import express from 'express'

const router = express.Router()

const app = express()
router.use((req, res, next) => {
  Object.setPrototypeOf(req, app.request)
  Object.setPrototypeOf(res, app.response)
  req.res = res
  res.req = req
  next()
})

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

export default {
  path: '/api/guilds',
  handler: router
}

1 个答案:

答案 0 :(得分:1)

令牌应该是键值对象。

 async fetch() {
   const token = 'testToken'
   const guilds = await this.$axios.$post('/api/guilds', { token: token })
 }

然后您可以从serverMiddleware的req.body中获取它。