码迷,mamicode.com
首页 > Windows程序 > 详细

[Hapi.js] Request Validation with Joi

时间:2016-03-01 06:20:08      阅读:248      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!