首页 > Windows开发 > 详细

[Hapi.js] Request Validation with Joi

时间:2016-03-01 06:19:01      阅读:232      评论:0      收藏:0      [点我收藏+]

hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi‘s simple, 

 

‘use strict‘
const Hapi = require(‘hapi‘)
const Joi = require(‘joi‘)
const server = new Hapi.Server()
server.connection({ port: 8000 })

server.route({
  method: [‘POST‘,‘PUT‘],
  path: ‘/user/{id?}‘,
  config: {
    validate: {
      params: Joi.object().keys({
        id: Joi.number()
      }),
      payload: Joi.object().keys({
        id: Joi.number()
        email: Joi.string()
      }).unknown(),
      query: Joi.object().keys({
        id: Joi.number()
      })
    },
    handler: function(request, reply) {
      reply({
        params: request.params,
        query: request.query
        payload: request.payload
      })
    }
  }
})

server.start(() => console.log(`Started at: ${server.info.uri}`))

 

[Hapi.js] Request Validation with Joi

原文:http://www.cnblogs.com/Answer1215/p/5229554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!