为什么 bodyParser returns 未定义?
Why bodyParser returns undefined?
我无法获取 POST http://127.0.0.1:3001/users?name=Slava 的请求正文。
服务器响应 'name is required'。方法 getUsers 工作正常。 RethinkDB 运行良好,server.js 也运行良好。我在这里搜索了类似的答案,但没有合适的。有很老的答案,但它们不相关。
这是请求:http://127.0.0.1:3001/users?name=bob(我使用 Postman POST)
为什么 bodyParser 在我的代码中不起作用?我不知道为什么会这样。
const Koa = require('koa')
const logger = require('koa-morgan')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
const r = require('rethinkdb')
const server = new Koa()
const router = new Router()
const db = async() => {
const connection = await r.connect({
host: 'localhost',
port: '28015',
db: 'getteamDB'
})
return connection;
}
server.use(bodyParser());
const insertUser = async(ctx, next) => {
await next()
// Get the db connection.
const connection = await db()
// Throw the error if the table does not exist.
var exists = await r.tableList().contains('users').run(connection)
if (exists === false) {
ctx.throw(500, 'users table does not exist')
}
let body = ctx.request.body || {}
console.log(body);
// Throw the error if no name.
if (body.name === undefined) {
ctx.throw(400, 'name is required')
}
// Throw the error if no email.
if (body.email === undefined) {
ctx.throw(400, 'email is required')
}
let document = {
name: body.name,
email: body.email
}
var result = await r.table('users')
.insert(document, {returnChanges: true})
.run(connection)
ctx.body = result
}
router
.post('/users', insertUser)
server
.use(router.routes())
.use(router.allowedMethods())
.use(logger('tiny')).listen(3001)
body parser用于解析POST请求(for POST body),这里要用req.query
代替req.body
,跟进this问题。
我无法获取 POST http://127.0.0.1:3001/users?name=Slava 的请求正文。
服务器响应 'name is required'。方法 getUsers 工作正常。 RethinkDB 运行良好,server.js 也运行良好。我在这里搜索了类似的答案,但没有合适的。有很老的答案,但它们不相关。
这是请求:http://127.0.0.1:3001/users?name=bob(我使用 Postman POST)
为什么 bodyParser 在我的代码中不起作用?我不知道为什么会这样。
const Koa = require('koa')
const logger = require('koa-morgan')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
const r = require('rethinkdb')
const server = new Koa()
const router = new Router()
const db = async() => {
const connection = await r.connect({
host: 'localhost',
port: '28015',
db: 'getteamDB'
})
return connection;
}
server.use(bodyParser());
const insertUser = async(ctx, next) => {
await next()
// Get the db connection.
const connection = await db()
// Throw the error if the table does not exist.
var exists = await r.tableList().contains('users').run(connection)
if (exists === false) {
ctx.throw(500, 'users table does not exist')
}
let body = ctx.request.body || {}
console.log(body);
// Throw the error if no name.
if (body.name === undefined) {
ctx.throw(400, 'name is required')
}
// Throw the error if no email.
if (body.email === undefined) {
ctx.throw(400, 'email is required')
}
let document = {
name: body.name,
email: body.email
}
var result = await r.table('users')
.insert(document, {returnChanges: true})
.run(connection)
ctx.body = result
}
router
.post('/users', insertUser)
server
.use(router.routes())
.use(router.allowedMethods())
.use(logger('tiny')).listen(3001)
body parser用于解析POST请求(for POST body),这里要用req.query
代替req.body
,跟进this问题。