Rumah  >  Soal Jawab  >  teks badan

node.js - Menggunakan koa2 + koa-router, tetapi request.body permintaan siaran tidak boleh diperolehi

Xiaobai meminta nasihat Apabila menghantar borang pada '/' untuk menghantar permintaan pos, tetapi badan yang dikembalikan oleh '/link' mempunyai objek request.body{}Mengapa ini?

const koa = require('koa')

const router = require('koa-router')()

const koaBody = require('koa-body')()

const bodyParser = require('koa-bodyparser')

const app = new koa()

app.use(bodyParser())

app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} -> ${ctx.url} - ${ms}ms`)
})

router.get('/',async (ctx, next) => {
  await next()
  ctx.body = `<form action='/link' method='post'><input type="text" ><input type="submit" value="OK"></form>`
})

router.get('/hello/:name', async ctx => {
  let name = ctx.params.name
  ctx.body = `<h1>Hello, ${name}!</h1>`
})

app.use(router.routes())

router.post('/link', async (ctx, next) => {
  
  ctx.response.body = {
    code:'0',
    description: 'ok',
    result: ctx.request.body
  }
})


// app.use(async ctx => {
//    ctx.body = 'hello koa2'
// })

app.listen(3000)
我想大声告诉你我想大声告诉你2652 hari yang lalu1282

membalas semua(1)saya akan balas

  • typecho

    typecho2017-06-17 09:18:49

    Hmm

    ctx.body = `<form action='/link' method='post'><input name="name" type="text" ><input type="submit" value="OK"></form>`

    Kotak input mesti mempunyai nama

    balas
    0
  • Batalbalas