{"id":294660,"date":"2019-09-13T09:01:31","date_gmt":"2019-09-13T09:01:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=294660"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=294660","title":{"rendered":"Hapi \u0434\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>Hapi.js \u2014 \u044d\u0442\u043e \u0444\u0440\u0435\u0439\u043c\u0444\u043e\u0440\u043a \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f web-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0443 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e \u0432\u0441\u0451 \u0441\u0430\u043c\u043e\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0433\u043e\u0440\u044f\u0447\u0435\u0433\u043e \u0441\u0442\u0430\u0440\u0442\u0430. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0430\u0432\u0442\u043e\u0440 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044c, \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u043c\u0430\u043b\u043e \u0441\u043b\u043e\u0432.<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"#link_mvp\">MVP<\/a><\/li>\n<li><a href=\"#link_inthewild\">In the wild<\/a><br \/> \n<ul>\n<li><a href=\"#link_sequelize\">ORM sequelize<\/a><\/li>\n<li><a href=\"#link_inject\">\u0418\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441<\/a><\/li>\n<li><a href=\"#link_auth\">\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<li><a href=\"#link_errorhandle\">\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/a><\/li>\n<\/ul>\n<p>  <\/li>\n<li><a href=\"#link_schemes\">\u0421\u0445\u0435\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/a><\/li>\n<li><a href=\"#link_swagger\">Swagger\/OpenAPI<\/a><\/li>\n<li><a href=\"#link_autotest\">\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0430\u0432\u0442\u043e\u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a><br \/>  <a name=\"link_mvp\"><\/a>  <\/p>\n<h2>MVP<\/h2>\n<p>  <\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c \u043f\u0430\u0447\u043a\u0443 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">npm i @hapi\/hapi @hapi\/boom filepaths hapi-boom-decorators<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/habr.com\/en\/users\/hapi\/\" class=\"user_link\">hapi<\/a>\/hapi \u2014 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440<\/li>\n<li><a href=\"https:\/\/habr.com\/en\/users\/hapi\/\" class=\"user_link\">hapi<\/a>\/boom \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/li>\n<li>hapi-boom-decorators \u2014 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a \u0434\u043b\u044f <a href=\"https:\/\/habr.com\/en\/users\/hapi\/\" class=\"user_link\">hapi<\/a>\/boom<\/li>\n<li>filepaths \u2014 \u0443\u0442\u0438\u043b\u0438\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0447\u0438\u0442\u0430\u0435\u0442 \u043f\u0430\u043f\u043a\u0438<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0430\u043f\u043e\u043a \u0438 \u043f\u0430\u0447\u043a\u0443 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/eh\/ic\/bk\/ehicbksl-1lcrvoenxe6rq9u-zw.png\"><\/p>\n<p>\u0412 .\/src\/routes\/ \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0430\u043f\u0438 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432, 1 \u0444\u0430\u0439\u043b \u2014 1 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ .\/src\/routes\/home.js  async function response() {   \/\/ content-type \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442\u0442\u043e\u0433\u043e \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445  \u0432 \u043e\u0442\u0432\u0435\u0442\u0435   return {     result: 'ok',     message: 'Hello World!'   }; }  module.exports = {   method: 'GET', \/\/ \u041c\u0435\u0442\u043e\u0434   path: '\/', \/\/ \u041f\u0443\u0442\u044c   options: {      handler: response \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0434\u043b\u044f hapi &gt; 17 \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u0440\u043e\u043c\u0438\u0441   } }; <\/code><\/pre>\n<p>  <\/p>\n<p>.\/src\/server.js \u2014 \u043c\u043e\u0434\u0443\u043b\u044c, \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ .\/src\/server.js 'use strict';  const Hapi = require('@hapi\/hapi'); const filepaths = require('filepaths'); const hapiBoomDecorators = require('hapi-boom-decorators');  const config = require('..\/config');  async function createServer() {   \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440   const server = await new Hapi.Server(config.server);    \/\/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435   await server.register([     hapiBoomDecorators   ]);    \/\/ \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0432\u0441\u0435 \u0440\u0443\u0442\u044b \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 .\/src\/routes\/   let routes = filepaths.getSync(__dirname + '\/routes\/');   for(let route of routes)     server.route( require(route) );      \/\/ \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440   try {     await server.start();     console.log(`Server running at: ${server.info.uri}`);   } catch(err) { \/\/ \u0435\u0441\u043b\u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u0442\u044c, \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443     console.log(JSON.stringify(err));   }    \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440   return server; }  module.exports = createServer;  <\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 .\/server.js \u0432\u0441\u0451, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u2014 \u044d\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c createServer()<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">#!\/usr\/bin\/env node  const createServer = require('.\/src\/server');  createServer(); <\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> node server.js <\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> curl http:\/\/127.0.0.1:3030\/ {\"result\":\"ok\",\"message\":\"Hello World!\"}  curl http:\/\/127.0.0.1:3030\/test {\"statusCode\":404,\"error\":\"Not Found\",\"message\":\"Not Found\"} <\/code><\/pre>\n<p>  <a name=\"link_inthewild\"><\/a>  <\/p>\n<h2>In the wild<\/h2>\n<p>  <\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043d\u0430\u043c, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c, \u043d\u0443\u0436\u043d\u044b \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043b\u043e\u0433\u0433\u0435\u0440, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0435\u0449\u0451.<\/p>\n<p>  <a name=\"link_sequelize\"><\/a>  <\/p>\n<h3>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c sequelize<\/h3>\n<p>  <\/p>\n<p>ORM sequelize \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043c\u043e\u0434\u0443\u043b\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">... const Sequelize = require('sequelize'); ... await server.register([ ...     {       plugin: require('hapi-sequelizejs'),       options: [         {           name: config.db.database, \/\/ identifier           models: [__dirname + '\/models\/*.js'], \/\/ \u041f\u0443\u0442\u044c \u043a \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0430\u043c           \/\/ignoredModels: [__dirname + '\/server\/models\/**\/*.js'], \/\/ \u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0435\u043a \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0438\u0433\u043d\u043e\u0440\u0438\u0442\u044c           sequelize: new Sequelize(config.db), \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f           sync: true, \/\/ default false           forceSync: false, \/\/ force sync (drops tables) - default false         },       ]     } ...   ]); <\/code><\/pre>\n<p>  <\/p>\n<p>\u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u043e\u0443\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">async function response(request) {   const model = request.getModel('\u0438\u043c\u044f_\u0431\u0434', '\u0438\u043c\u044f_\u0442\u0430\u0431\u043b\u0438\u0446\u044b'); } <\/code><\/pre>\n<p>  <a name=\"link_inject\"><\/a>  <\/p>\n<h3>\u0418\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441<\/h3>\n<p>  <\/p>\n<p>\u041d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u00abonRequest\u00bb, \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 request \u0437\u0430\u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433 \u0438 \u043b\u043e\u0433\u0433\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">... const Logger = require('.\/libs\/Logger'); ... async function createServer(logLVL=config.logLVL) {   ...   const logger = new Logger(logLVL, 'my-hapi-app');   ...   server.ext({     type: 'onRequest',     method: async function (request, h) {       request.server.config = Object.assign({}, config);       request.server.logger = logger;       return h.continue;     }   });   ... }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043a\u043e\u043d\u0444\u0438\u0433, \u043b\u043e\u0433\u0433\u0435\u0440, \u0438 \u0431\u0434, \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u0438\u043d\u043a\u043b\u044e\u0434\u0438\u0442\u044c \u0432 \u0442\u0435\u043b\u0435 \u043c\u043e\u0434\u0443\u043b\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ .\/src\/routes\/home.js  async function response(request) {   \/\/ \u041b\u043e\u0433\u0433\u0435\u0440   request.server.logger.error('request error', 'something went wrong');      \/\/ \u041a\u043e\u043d\u0444\u0438\u0433   console.log(request.server.config);      \/\/ \u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445   const messages = request.getModel(request.server.config.db.database, '\u0438\u043c\u044f_\u0442\u0430\u0431\u043b\u0438\u0446\u044b');      return {     result: 'ok',     message: 'Hello World!'   }; }  module.exports = {   method: 'GET', \/\/ \u041c\u0435\u0442\u043e\u0434   path: '\/', \/\/ \u041f\u0443\u0442\u044c   options: {      handler: response \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0434\u043b\u044f hapi &gt; 17 \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u0440\u043e\u043c\u0438\u0441   } }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0432\u0441\u0451 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0438 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0438\u043d\u043a\u043b\u044e\u0434\u0438\u0442\u044c \u043e\u0434\u043d\u0438 \u0438 \u0442\u0435 \u0436\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0438\u0437 \u0440\u0430\u0437\u0430 \u0432 \u0440\u0430\u0437.<\/p>\n<p>  <a name=\"link_auth\"><\/a>  <\/p>\n<h3>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  <\/p>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432 hapi \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0432 \u0432\u0438\u0434\u0435 \u043c\u043e\u0434\u0443\u043b\u0435\u0439.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">... const AuthBearer = require('hapi-auth-bearer-token'); ... async function createServer(logLVL=config.logLVL) {   ...   await server.register([     AuthBearer,     ...   ]);      server.auth.strategy('token', 'bearer-access-token', {\/\/ 'token' - \u044d\u0442\u043e \u0438\u043c\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435     allowQueryToken: false,     unauthorized: function() { \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u0438\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 validate \u0432\u0435\u0440\u043d\u0443\u043b isValid=false       throw Boom.unauthorized();     },     validate: function(request, token) {       if( token == 'asd' ) {         return { \/\/ \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d           isValid: true,           credentials: {}         };       } else {         return { \/\/ \u0415\u0441\u043b\u0438 \u043d\u0435\u0442           isValid: false,           credentials: {}         };       }     }   });      server.auth.default('token'); \/\/ \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e   ... } <\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u043e\u0443\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u0432\u0438\u0434 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">module.exports = {   method: 'GET',   path: '\/',   auth: 'token', \/\/ \u043b\u0438\u0431\u043e false, \u0435\u0441\u043b\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430   options: {      handler: response    } }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">auth: {   strategies: ['token1', 'token2', 'something_else'] }, <\/code><\/pre>\n<p>  <a name=\"link_errorhandle\"><\/a>  <\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h3>\n<p>  <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, boom \u0432\u044b\u0434\u0430\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0442\u0438\u043f\u043e\u0432\u043e\u043c \u0432\u0438\u0434\u0435, \u0447\u0430\u0441\u0442\u043e \u044d\u0442\u0438 \u043e\u0442\u0432\u0435\u0442\u044b \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0432 \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">server.ext('onPreResponse', function (request, h) {   \/\/ \u0415\u0441\u043b\u0438 \u043e\u0442\u0432\u0435\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u043b \u043d\u0435 \u043e\u0442 Boom, \u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c   if ( !request.response.isBoom ) {     return h.continue;   }      \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0441\u0432\u043e\u0451 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435   let responseObj = {     message: request.response.output.statusCode === 401 ? 'AuthError' : 'ServerError',     status: request.response.message   }      \/\/ \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e \u043b\u043e\u0433   logger.error('code: ' + request.response.output.statusCode, request.response.message);      return h.response(responseObj).code(request.response.output.statusCode); }); <\/code><\/pre>\n<p>  <a name=\"link_schemes\"><\/a>  <\/p>\n<h2>\u0421\u0445\u0435\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u0442\u0435\u043c\u0430. \u0421\u0445\u0435\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430. \u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0412\u044b \u043e\u043f\u0438\u0448\u0438\u0442\u0435 \u044d\u0442\u0438 \u0441\u0445\u0435\u043c\u044b, \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0431\u0443\u0434\u0443\u0442 swagger \u0438 \u0430\u0432\u0442\u043e\u0442\u0435\u0441\u0442\u044b.<\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u0441\u0445\u0435\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 joi. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const Joi = require('@hapi\/joi'); const Boom = require('boom');  async function response(request) {      \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0438   const accessTokens = request.getModel(request.server.config.db.database, 'access_tokens');   const users = request.getModel(request.server.config.db.database, 'users');      \/\/ \u0418\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043f\u043e\u0447\u0442\u0435   let userRecord = await users.findOne({ where: { email: request.query.login } });    \/\/ \u0435\u0441\u043b\u0438 \u043d\u0435 \u043d\u0430\u0448\u043b\u0438, \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u0447\u0442\u043e \u043d\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u044b   if ( !userRecord ) {     throw Boom.unauthorized();   }      \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u0438   if ( !userRecord.verifyPassword(request.query.password) ) {     throw Boom.unauthorized();\/\/ \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043e\u043f\u044f\u0442\u044c \u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c, \u0447\u0442\u043e \u043d\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u044b   }      \/\/ \u0418\u043d\u0430\u0447\u0435, \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043d\u043e\u0432\u044b\u0439 \u0442\u043e\u043a\u0435\u043d   let token = await accessTokens.createAccessToken(userRecord);      \/\/ \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0435\u0433\u043e   return {     meta: {       total: 1     },     data: [ token.dataValues ]   }; }  \/\/ \u043f\u043e\u0434\u0441\u0445\u0435\u043c\u0430 \u0434\u043b\u044f \u0442\u043e\u043a\u0435\u043d\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u043b\u043e\u0436\u0438\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 const tokenScheme = Joi.object({   id: Joi.number().integer().example(1),   user_id: Joi.number().integer().example(2),   expires_at: Joi.date().example('2019-02-16T15:38:48.243Z'),   token: Joi.string().example('4443655c28b42a4349809accb3f5bc71'),   updatedAt: Joi.date().example('2019-02-16T15:38:48.243Z'),   createdAt: Joi.date().example('2019-02-16T15:38:48.243Z') });  \/\/ \u0421\u0445\u0435\u043c\u0430 \u043e\u0442\u0432\u0435\u0442\u0430 const responseScheme = Joi.object({   meta: Joi.object({     total: Joi.number().integer().example(3)   }),   data: Joi.array().items(tokenScheme) });  \/\/ \u0421\u0445\u0435\u043c\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 const requestScheme =Joi.object({   login: Joi.string().email().required().example('pupkin@gmail.com'),   password: Joi.string().required().example('12345') });  module.exports = {   method: 'GET',   path: '\/auth',   options: {     handler: response,     validate: {       query: requestScheme     },     response: { schema: responseScheme }   } }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> curl -X GET \"http:\/\/localhost:3030\/auth?login=pupkin@gmail.com&amp;password=12345\" <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/rt\/qh\/ta\/rtqhtahka6wz7937bkfictdgy_0.png\"><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u0447\u0442\u044b, \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u043e\u0433\u0438\u043d:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> curl -X GET \"http:\/\/localhost:3030\/auth?login=pupkin&amp;password=12345\" <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/za\/w2\/4l\/zaw24l9f3si71uxkfqutwzj5uas.png\"><\/p>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0442\u0432\u0435\u0442 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0445\u0435\u043c\u0435 \u043e\u0442\u0432\u0435\u0442\u0430, \u0442\u043e \u0441\u0435\u0440\u0432\u0435\u0440 \u0442\u0430\u043a\u0436\u0435, \u0432\u044b\u0432\u0430\u043b\u0438\u0442\u0441\u044f \u0432 500 \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>  <\/p>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u0442\u0430\u043b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c 1 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0447\u0430\u0441, \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043b\u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043e\u0442\u0432\u0435\u0442\u043e\u0432, \u0442.\u043a. \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u2014 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0451\u043c\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440: \u00absample\u00bb<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">module.exports = {   method: 'GET',   path: '\/auth',   options: {     handler: response,     validate: {       query: requestScheme     },     response: { sample: 50, schema: responseScheme }   } }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435 \u0442\u043e\u043b\u044c\u043a\u043e 50% \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u043e\u0442\u0432\u0435\u0442\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>  <a name=\"link_swagger\"><\/a>  <\/p>\n<h2>Swagger\/OpenAPI<\/h2>\n<p>  <\/p>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043f\u0430\u0447\u043a\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> npm i hapi-swagger @hapi\/inert @hapi\/vision <\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0438\u0445 \u0432 server.js<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">... const Inert = require('@hapi\/inert'); const Vision = require('@hapi\/vision'); const HapiSwagger = require('hapi-swagger'); const Package = require('..\/package'); ... const swaggerOptions = {   info: {     title: Package.name + ' API Documentation',     description: Package.description   },   jsonPath: '\/documentation.json',   documentationPath: '\/documentation',   schemes: ['https', 'http'],   host: config.swaggerHost,   debug: true }; ... async function createServer(logLVL=config.logLVL) {   ...   await server.register([     ...     Inert,     Vision,     {       plugin: HapiSwagger,       options: swaggerOptions     },     ...   ]);   ... }); <\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u043e\u0443\u0442\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u0433 \u00abapi\u00bb:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">module.exports = {   method: 'GET',   path: '\/auth',   options: {     handler: response,     tags: [ 'api' ], \/\/ \u042d\u0442\u043e\u0442 \u0442\u0435\u0433 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 swagger'\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u043e\u0443\u0442 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e     validate: {       query: requestScheme     },     response: { sample: 50, schema: responseScheme }   } }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"http:\/\/localhost:3030\/documentation\">http:\/\/localhost:3030\/documentation<\/a> \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432\u0435\u0431-\u043c\u043e\u0440\u0434\u043e\u0447\u043a\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439, \u0430 \u043f\u043e <a href=\"http:\/\/localhost:3030\/documentation.json\">http:\/\/localhost:3030\/documentation.json<\/a> .json \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tp\/ht\/b3\/tphtb36liamqxbzsvlgxglbmle4.png\"><br \/>  <a name=\"link_autotest\"><\/a>  <\/p>\n<h2>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0430\u0432\u0442\u043e\u0442\u0435\u0441\u0442\u043e\u0432<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043b\u0438 \u0441\u0445\u0435\u043c\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432, \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 seed \u0431\u0430\u0437\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c, \u043e\u043f\u0438\u0441\u0430\u043d\u044b\u043c \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0442\u043e, \u043f\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c \u0441\u0445\u0435\u043c\u0430\u043c, \u043c\u043e\u0436\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043a\u043e\u0434\u044b \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 GET:\/auth \u043e\u0436\u0438\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b login \u0438 password, \u0438\u0445 \u043c\u044b \u0432\u043e\u0437\u044c\u043c\u0451\u043c \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u0441\u0445\u0435\u043c\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const requestScheme =Joi.object({   login: Joi.string().email().required().example('pupkin@gmail.com'),   password: Joi.string().required().example('12345') }); <\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0435\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0432\u0435\u0442\u0438\u0442 HTTP-200-OK, \u0442\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u0435\u0441\u0442 \u043f\u0440\u043e\u0439\u0434\u0435\u043d.<\/p>\n<p>  <\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0433\u043e\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f \u043d\u0435 \u043d\u0430\u0448\u043b\u043e\u0441\u044c, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0433\u043e\u0432\u043d\u043e\u043a\u043e\u0434\u0438\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ .\/test\/autogenerate.js  const assert = require('assert'); const rp = require('request-promise'); const filepaths = require('filepaths'); const rsync = require('sync-request');  const config = require('.\/..\/config'); const createServer = require('..\/src\/server');  const API_URL = 'http:\/\/0.0.0.0:3030'; const AUTH_USER = { login: 'pupkin@gmail.com', pass: '12345' };  const customExamples = {   'string': 'abc',   'number': 2,   'boolean': true,   'any': null,   'date': new Date() };  const allowedStatusCodes = {   200: true,   404: true };  function getExampleValue(joiObj) {   if( joiObj == null ) \/\/ if joi is null     return joiObj;    if( typeof(joiObj) != 'object' ) \/\/If it's not joi object     return joiObj;    if( typeof(joiObj._examples) == 'undefined' )     return customExamples[ joiObj._type ];    if( joiObj._examples.length &lt;= 0 )     return customExamples[ joiObj._type ];    return joiObj._examples[ 0 ].value; }  function generateJOIObject(schema) {    if( schema._type == 'object' )     return generateJOIObject(schema._inner.children);    if( schema._type == 'string' )     return getExampleValue(schema);    let result = {};   let _schema;    if( Array.isArray(schema) ) {     _schema = {};     for(let item of schema) {       _schema[ item.key ] = item.schema;     }   } else {     _schema = schema;   }    for(let fieldName in _schema) {      if( _schema[ fieldName ]._type == 'array' ) {       result[ fieldName ] = [ generateJOIObject(_schema[ fieldName ]._inner.items[ 0 ]) ];     } else {       if( Array.isArray(_schema[ fieldName ]) ) {         result[ fieldName ] = getExampleValue(_schema[ fieldName ][ 0 ]);       } else if( _schema[ fieldName ]._type == 'object' ) {         result[ fieldName ] = generateJOIObject(_schema[ fieldName ]._inner);       } else {         result[ fieldName ] = getExampleValue(_schema[ fieldName ]);       }     }   }    return result }  function generateQuiryParams(queryObject) {   let queryArray = [];   for(let name in queryObject)     queryArray.push(`${name}=${queryObject[name]}`);    return queryArray.join('&amp;'); }  function generatePath(basicPath, paramsScheme) {   let result = basicPath;    if( !paramsScheme )     return result;    let replaces = generateJOIObject(paramsScheme);    for(let key in replaces)     result = result.replace(`{${key}}`, replaces[ key ]);    return result; }  function genAuthHeaders() {   let result = {};    let respToken = rsync('GET', API_URL + `\/auth?login=${AUTH_USER.login}&amp;password=${AUTH_USER.pass}`);   let respTokenBody = JSON.parse(respToken.getBody('utf8'));      result[ 'token' ] = {     Authorization: 'Bearer ' + respTokenBody.data[ 0 ].token   };      return result; }  function generateRequest(route, authKeys) {    if( !route.options.validate ) {     return false;   }    let options = {     method: route.method,     url: API_URL + generatePath(route.path, route.options.validate.params) + '?' + generateQuiryParams( generateJOIObject(route.options.validate.query || {}) ),     headers: authKeys[ route.options.auth ]  ? authKeys[ route.options.auth ] : {},     body: generateJOIObject(route.options.validate.payload || {}),     json: true,     timeout: 15000   }    return options; }  let authKeys = genAuthHeaders(); let testSec = [ 'POST', 'PUT', 'GET', 'DELETE' ]; let routeList = [];  for(let route of filepaths.getSync(__dirname + '\/..\/src\/routes\/'))   routeList.push(require(route));  describe('Autogenerate Hapi Routes TEST', async () =&gt; {    for(let metod of testSec)   for(let testRoute of routeList) {     if( testRoute.method != metod ) {       continue;     }      it(`TESTING: ${testRoute.method} ${testRoute.path}`,  async function () {       let options = generateRequest(testRoute, authKeys);        if( !options )         return false;        let statusCode = 0;        try {         let result = await rp( options );         statusCode = 200;       } catch(err) {         statusCode = err.statusCode;       }        if( !allowedStatusCodes[ statusCode ] ) {         console.log('*** TEST STACK FOR:', `${testRoute.method} ${testRoute.path}`);         console.log('options:', options);         console.log('StatusCode:', statusCode);       }        return assert.ok(allowedStatusCodes[ statusCode ]);     });    }  }); <\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> npm i request-promise mocha sync-request <\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u043f\u0440\u043e package.json<\/p>\n<p>  <\/p>\n<pre><code class=\"json\">...   \"scripts\": {     \"test\": \"mocha\",     \"dbinit\": \"node .\/scripts\/dbInit.js\"   }, ... <\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\"> npm test <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/8a\/do\/jg\/8adojgir1rzh5gj1jysb1aqnvza.png\"><\/p>\n<p>\u0410 \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u043e\u0440\u043e\u0442\u0430 \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u0441\u0445\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043b\u0438\u0431\u043e \u043e\u0442\u0432\u0435\u0442 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0445\u0435\u043c\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/py\/jz\/1i\/pyjz1ithtglj9o-bnsa6b9esoqg.png\"><\/p>\n<p>\u0418 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0442\u0435\u0441\u0442\u044b \u0441\u0442\u0430\u043d\u0443\u0442 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u043b\u0435\u0436\u0430\u0449\u0438\u043c \u0432 \u0431\u0434. \u041f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432 \u043d\u0443\u0436\u043d\u043e, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c, \u0432\u0430\u0439\u043f\u0430\u0442\u044c \u0431\u0430\u0437\u0443.<\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/gitlab.com\/hololoev\/hapi_howto_example\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a><\/p>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/467099\/\"> https:\/\/habr.com\/ru\/post\/467099\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>Hapi.js \u2014 \u044d\u0442\u043e \u0444\u0440\u0435\u0439\u043c\u0444\u043e\u0440\u043a \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f web-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0443 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e \u0432\u0441\u0451 \u0441\u0430\u043c\u043e\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0433\u043e\u0440\u044f\u0447\u0435\u0433\u043e \u0441\u0442\u0430\u0440\u0442\u0430. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0430\u0432\u0442\u043e\u0440 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044c, \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u043c\u0430\u043b\u043e \u0441\u043b\u043e\u0432.<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"#link_mvp\">MVP<\/a><\/li>\n<li><a href=\"#link_inthewild\">In the wild<\/a><br \/> \n<ul>\n<li><a href=\"#link_sequelize\">ORM sequelize<\/a><\/li>\n<li><a href=\"#link_inject\">\u0418\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441<\/a><\/li>\n<li><a href=\"#link_auth\">\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/a><\/li>\n<li><a href=\"#link_errorhandle\">\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/a><\/li>\n<\/ul>\n<p>  <\/li>\n<li><a href=\"#link_schemes\">\u0421\u0445\u0435\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/a><\/li>\n<li><a href=\"#link_swagger\">Swagger\/OpenAPI<\/a><\/li>\n<li><a href=\"#link_autotest\">\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0430\u0432\u0442\u043e\u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-294660","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294660"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294660\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}