项目作者: nasa8x

项目描述 :
Hapi.js body parsing plugin
高级语言: JavaScript
项目地址: git://github.com/nasa8x/hapi-bodyparser.git
创建时间: 2017-04-01T07:30:09Z
项目社区:https://github.com/nasa8x/hapi-bodyparser

开源协议:MIT License

下载


hapi-bodyparser

Hapi.js body parsing plugin support merge querystring, sub objects and sanitizer.

Parse incoming request bodies in a plugin before your handlers, available under the request.payload (body: true —> request.body) property.

If you don’t know Node.js

Node.js Tutorial for Beginners in 2020

How To Build a Blog with Nest.js, MongoDB, and Vue.js

Machine Learning In Node.js With TensorFlow.js

  1. npm install hapi-bodyparser --save
  1. // Not set if you want default options
  2. options: {
  3. // parser options use qs.parse(value, options)
  4. parser: { allowDots: true, strictNullHandling: true },
  5. sanitizer: {
  6. trim: true, // remove first || end white space of String
  7. stripNullorEmpty: true // remove property when Null or Empty
  8. },
  9. merge: false, // merge querystring into body
  10. body: false // If false: request.payload is default parsed | if true request.body is parsed
  11. }
  1. var Hapi = require('hapi');
  2. var server = new Hapi.Server();
  3. server.connection({ port: 8080, host: 'localhost' });
  4. server.register([{
  5. register: require('hapi-bodyparser'),
  6. options: {
  7. // parser: { allowDots: true, strictNullHandling: true },
  8. // sanitizer: {
  9. // trim: true,
  10. // stripNullorEmpty: true
  11. // },
  12. // merge: false,
  13. // body: false
  14. }
  15. }], function (err) {
  16. // Insert your preferred error handling here...
  17. });

Options can be configured on a route via the body plugin object.

  1. server.route({
  2. method: 'POST',
  3. path: '/api/post/fetch',
  4. options: {
  5. plugins: {
  6. body: { merge: false, sanitizer: { stripNullorEmpty: false } }
  7. },
  8. handler: function (request, reply) {
  9. reply(request.payload);
  10. }
  11. }
  12. });

Parsing sub object for validate dynamic object keys name

  1. server.route({
  2. method: 'POST',
  3. path: '/api/post/fetch',
  4. options: {
  5. auth: {
  6. strategy: 'session',
  7. mode: 'try'
  8. },
  9. validate: {
  10. payload: {
  11. limit: Joi.number(),
  12. offset: Joi.number(),
  13. sort: Joi.object().pattern(/.*/, Joi.alternatives().try(Joi.number(), Joi.boolean(), Joi.string())),
  14. search: Joi.string().allow('')
  15. }
  16. },
  17. handler: (request, reply) => {
  18. var _uid = request.auth.credentials._id;
  19. var _limit = request.payload.limit;
  20. var _offset = request.payload.offset;
  21. var _sort = request.payload.sort;
  22. var _kwd = request.payload.search;
  23. var _condition = [{ _uid: _uid }];
  24. if(_kwd.length>0){
  25. _condition.push({
  26. $or: [
  27. { tl: new RegExp(_kwd, "ig") },
  28. { desc: new RegExp(_kwd, "ig") }
  29. ]
  30. })
  31. }
  32. Post.paginate({ $and: _condition }, {
  33. offset: _offset,
  34. limit: _limit,
  35. sort: _sort
  36. },
  37. function (err, result) {
  38. if (result) {
  39. reply({
  40. total: result.total,
  41. rows: result.docs
  42. });
  43. } else {
  44. reply({ total: 0, rows: [] });
  45. }
  46. });
  47. }
  48. }
  49. });

With option merge: true, merge querystring into payload (body).

  1. server.route({
  2. method: 'POST',
  3. path: '/api/post/fetch?abc=1',
  4. options: {
  5. plugins: {
  6. body: { merge: true }
  7. },
  8. handler: function (request, reply) {
  9. reply(request.payload);
  10. // return {abc: 1, ...}
  11. }
  12. }
  13. });

Option sanitizer help clean object properties.

  1. // with default options: trim: true and stripNullorEmpty: true
  2. {
  3. a: ' Hello ',
  4. b: '',
  5. c: null,
  6. d: 'World ',
  7. e: {
  8. a: null,
  9. b: 1,
  10. c:''
  11. }
  12. }
  13. // after sanitize
  14. {
  15. a: 'Hello',
  16. d: 'World',
  17. e: {
  18. b: 1
  19. }
  20. }