Node.js 无法将hapi swagger注册到hapi rest api最新版本17
一年前,我使用以下版本的Node.js 无法将hapi swagger注册到hapi rest api最新版本17,node.js,rest,hapijs,hapi-swagger,Node.js,Rest,Hapijs,Hapi Swagger,一年前,我使用以下版本的hapi和hapi-swagger创建了一个RESTapi hapi: "8.5.1", hapi-swagger: "0.7.3" 为了注册hapi-swagger,我使用了以下注册选项: server.register([{ register: require('hapi-swagger'), options: { apiVersion: "0.0.1" } }], function (err) { if (err)
hapi
和hapi-swagger
创建了一个REST
api
hapi: "8.5.1",
hapi-swagger: "0.7.3"
为了注册hapi-swagger,我使用了以下注册选项:
server.register([{
register: require('hapi-swagger'),
options: {
apiVersion: "0.0.1"
}
}], function (err) {
if (err) {
server.log(['error'], 'hapi-swagger load error: ' + err)
} else {
server.log(['start'], 'hapi-swagger interface loaded')
}
});
它与上述版本和代码配合得很好。我考虑将两者更新为最新版本,并更新为以下版本:
hapi: "17.0.2",
hapi-swagger: "8.0.0"
更新后,当我尝试运行服务器时,出现以下错误:
Exception has occurred: AssertionError
AssertionError: Invalid register options "value" must be an object
at Object.exports.apply (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/config.js:22:10)
at internals.Server.register (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/server.js:332:26)
at Object.<anonymous> (/Users/karthik/Desktop/new_gem/es6/CriApi/server.js:7:8)
at Module._compile (module.js:632:14)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
我尝试了这两种方法,但现在出现以下错误:
Exception has occurred: AssertionError
AssertionError: Invalid plugin options {
"plugin": {
"register": function (plugin, options, next) {\n\n\n let settings = Hoek.applyToDefaults(Defaults, options, true);\n const publicDirPath = Path.resolve(__dirname, '..', 'public');\n const swaggerDirPath = Path.join(publicDirPath, 'swaggerui');\n\n settings.log = (tags, data) => {\n\n tags.unshift('hapi-swagger');\n if (settings.debug) {\n plugin.log(tags, data);\n }\n };\n settings.log(['info'], 'Started');\n\n // add server method for caching\n if (settings.cache) {\n // set default\n settings.cache.segment = 'hapi-swagger';\n if (!settings.cache.generateTimeout) {\n settings.cache.generateTimeout = 30 * 1000;\n }\n\n plugin.method('getSwaggerJSON', Builder.getSwaggerJSON, {\n cache: settings.cache,\n generateKey: () => {\n\n return 'hapi-swagger';\n }\n });\n }\n\n\n // add routing swagger json\n plugin.route([{\n method: 'GET',\n path: settings.jsonPath,\n config: {\n auth: settings.auth,\n cors: settings.cors,\n handler: (request, reply) => {\n\n Joi.assert(settings, schema);\n\n if (settings.cache) {\n /*eslint no-unused-vars:0 */\n plugin.methods.getSwaggerJSON(settings, request, (err, json, cached, report) => {\n\n /* $lab:coverage:off$ */\n if (err) {\n reply(err);\n /* $lab:coverage:on$ */\n } else {\n //console.log(JSON.stringify(report));\n const lastModified = cached ? new Date(cached.stored) : new Date();\n reply(json).header('last-modified', lastModified.toUTCString());\n }\n });\n } else {\n Joi.assert(settings, schema);\n Builder.getSwaggerJSON(settings, request, (err, json) => {\n\n reply(json);\n });\n }\n },\n plugins: {\n 'hapi-swagger': false\n }\n }\n }]);\n\n\n // only add 'inert' and 'vision' based routes if needed\n if (settings.documentationPage === true || settings.swaggerUI === true) {\n\n // make sure we have other plug-in dependencies\n plugin.dependency(['inert', 'vision'], (pluginWithDependencies, nextWithDependencies) => {\n\n // add routing for swaggerui static assets /swaggerui/\n pluginWithDependencies.views({\n engines: {\n html: {\n module: require('handlebars')\n }\n },\n path: swaggerDirPath\n });\n\n // add documentation page\n if (settings.documentationPage === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: settings.documentationPath,\n config: {\n auth: settings.auth\n },\n handler: (request, reply) => {\n\n reply.view('index.html', {});\n }\n }]);\n }\n\n // add swagger UI if asked for or need by documentation page\n if (settings.documentationPage === true || settings.swaggerUI === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: settings.swaggerUIPath + '{path*}',\n config: {\n auth: false\n },\n handler: {\n directory: {\n path: swaggerDirPath + Path.sep,\n listing: true,\n index: false\n }\n }\n }, {\n method: 'GET',\n path: settings.swaggerUIPath + 'extend.js',\n config: {\n auth: false,\n files: {\n relativeTo: publicDirPath\n }\n },\n handler: {\n file: 'extend.js'\n }\n }]);\n }\n\n // add debug page\n if (settings.debug === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: Path.join(settings.documentationPath, Path.sep, 'debug').split(Path.sep).join('/'),\n config: {\n auth: settings.auth\n },\n handler: (request, reply) => {\n\n reply.view('debug.html', {}).type('application/json');\n }\n }]);\n }\n\n appendDataContext(pluginWithDependencies, settings);\n\n nextWithDependencies();\n\n });\n }\n\n // TODO: need to work how to test this as it need a request object\n // Undocument API interface, it may change\n /* $lab:coverage:off$ */\n plugin.expose('getJSON', function (exposeOptions, request, callback) {\n\n // use either options passed to function or plug-in scope options\n let exposeSettings = {};\n if (exposeOptions && Utilities.hasProperties(exposeOptions)) {\n exposeSettings = Hoek.applyToDefaults(Defaults, exposeOptions);\n Joi.assert(exposeSettings, schema);\n } else {\n exposeSettings = Hoek.clone(settings);\n }\n Builder.getSwaggerJSON(exposeSettings, request, callback);\n });\n /* $lab:coverage:on$ */\n\n\n next();\n},
[41m"name"[0m[31m [1]: -- missing --[0m
}
}
[31m
[1] "name" is required[0m
at Object.exports.apply (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/config.js:22:10)
at internals.Server.register (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/server.js:364:31)
at liftOff (/Users/karthik/Desktop/new_gem/es6/CriApi/server.js:30:18)
at Object.<anonymous> (/Users/karthik/Desktop/new_gem/es6/CriApi/server.js:36:1)
at Module._compile (module.js:632:14)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
发生异常:AssertionError
AssertionError:插件选项无效{
“插件”:{
“register”:函数(插件,选项,下一步){\n\n让设置=Hoek.applyToDefaults(默认值,选项,true);\n const publicDirPath=Path.resolve(uu dirname,…,,,,,,,,,,,,,,,,;\n const swaggerDirPath=Path.join(publicDirPath,,,swaggerui');\n\n\n settings=(标记,数据)=>{\n\n标记。unshift('hapi-swagger');\n如果(settings.debug){\n plugin.log(tags,data);\n}\n};\n settings.log(['info'],'Started');\n\n//添加缓存的服务器方法\n如果(settings.cache){\n//设置默认值\n settings.cache.segment='hapi-swagger';\n如果(!settings.cache.generateTimeout){\n settings.cache.generateTimeout=30*1000;\n}\n\n plugin.method('getSwaggerJSON',Builder.getSwaggerJSON,{\n cache:settings.cache,\n generateKey:()=>{\n\n返回'hapi-swagger';\n}\n});\n}\n\n\n\n//添加路由swagger-json\n plugin.route([{\n方法:'GET',\n路径:settings.jsonPath,\n配置:{\n auth:settings.auth,\n cors:settings.cors,\n处理程序:(请求,应答)=>{\n\n Joi.assert(设置,架构);\n\n if(settings.cache){\n/*eslint没有未使用的变量:0*/\n plugin.methods.getSwaggerJSON(设置、请求、(err、json、缓存、报告)=>{\n\n/*$lab:coverage:off$*/\n if(err){\n reply(err);\n/*$lab:coverage:on$*/\n}else{\n//console.log(JSON.stringify(report));\n const lastModified=cached?new Date(cached.stored):new Date();\n reply(JSON).header('last-modified',lastModified.toutString());\n}\n});\n}else{\n Joi.assert(设置,模式);\n Builder.getSwaggerJSON(设置,请求,(err,json)=>{\n\n reply(json);\n});\n}\n}\n}\n}\n插件:{\n'hapi-swagger':false\n}\n}\n}\n}]);\n\n\n//仅在需要时添加基于“惰性”和“视觉”的路由\n如果(settings.documentationPage==true | | settings.swaggerUI==true){\n\n//确保我们有其他插件依赖项\n plugin.dependency(['indirect',vision'],(pluginWithDependencies,nextWithDependencies)=>{\n\n//为swaggerui静态资产添加路由/swaggerui/\n pluginWithDependencies.views({\n引擎:{\n html:{\n模块:require('Handlebar')\n}\n},\n path:swaggerDirPath\n});\n\n//添加文档页\n如果(settings.documentationPage==true){\n pluginWithDependencies.route([{\n方法:'GET',\n路径:settings.documentationPath,\n配置:{\n auth:settings.auth\n},\n handler:(request,reply)=>{\n\n reply.view('index.html',{});\n}\n}\n}]);\n}\n\n//如果文档页面要求或需要,请添加招摇过市用户界面\n如果(settings.documentationPage==true | | settings.swaggerUI==true){\n pluginWithDependencies.route([{\n方法:'GET',\n路径:settings.swaggerUIPath+'{path*}',\n配置:{\n auth:false\n},\n处理程序:{\n目录:{\n路径:swaggerDirPath+path.sep,\n列表:true,\n索引:false\n}\n}\n},{\n方法:'GET',\n路径:settings.swaggerUIPath+'extend.js',\n配置:{\n auth:false,\n文件:{\n relativeTo:publicDirPath\n}\n},\n处理程序:{\n file:'extend.js'\n}\n}];\n}\n\n//添加调试页\n if(settings.debug==true){\n pluginWithDependencies.route([{\n方法:'GET',\n路径:path.join(settings.documentationPath,path.sep,'debug')).split(Path.sep).join('/'),\n config:{\n auth:settings.auth\n},\n handler:(请求,回复)=>{\n\n reply.view('debug.html',{}).type('application/json');\n}\n}]\n\n appendDataContext(pluginWithDependencies,settings);\n\n nextWithDependencies();\n\n});\n}\n\n//TODO:需要解决如何测试它,因为它需要一个
Exception has occurred: AssertionError
AssertionError: Invalid plugin options {
"plugin": {
"register": function (plugin, options, next) {\n\n\n let settings = Hoek.applyToDefaults(Defaults, options, true);\n const publicDirPath = Path.resolve(__dirname, '..', 'public');\n const swaggerDirPath = Path.join(publicDirPath, 'swaggerui');\n\n settings.log = (tags, data) => {\n\n tags.unshift('hapi-swagger');\n if (settings.debug) {\n plugin.log(tags, data);\n }\n };\n settings.log(['info'], 'Started');\n\n // add server method for caching\n if (settings.cache) {\n // set default\n settings.cache.segment = 'hapi-swagger';\n if (!settings.cache.generateTimeout) {\n settings.cache.generateTimeout = 30 * 1000;\n }\n\n plugin.method('getSwaggerJSON', Builder.getSwaggerJSON, {\n cache: settings.cache,\n generateKey: () => {\n\n return 'hapi-swagger';\n }\n });\n }\n\n\n // add routing swagger json\n plugin.route([{\n method: 'GET',\n path: settings.jsonPath,\n config: {\n auth: settings.auth,\n cors: settings.cors,\n handler: (request, reply) => {\n\n Joi.assert(settings, schema);\n\n if (settings.cache) {\n /*eslint no-unused-vars:0 */\n plugin.methods.getSwaggerJSON(settings, request, (err, json, cached, report) => {\n\n /* $lab:coverage:off$ */\n if (err) {\n reply(err);\n /* $lab:coverage:on$ */\n } else {\n //console.log(JSON.stringify(report));\n const lastModified = cached ? new Date(cached.stored) : new Date();\n reply(json).header('last-modified', lastModified.toUTCString());\n }\n });\n } else {\n Joi.assert(settings, schema);\n Builder.getSwaggerJSON(settings, request, (err, json) => {\n\n reply(json);\n });\n }\n },\n plugins: {\n 'hapi-swagger': false\n }\n }\n }]);\n\n\n // only add 'inert' and 'vision' based routes if needed\n if (settings.documentationPage === true || settings.swaggerUI === true) {\n\n // make sure we have other plug-in dependencies\n plugin.dependency(['inert', 'vision'], (pluginWithDependencies, nextWithDependencies) => {\n\n // add routing for swaggerui static assets /swaggerui/\n pluginWithDependencies.views({\n engines: {\n html: {\n module: require('handlebars')\n }\n },\n path: swaggerDirPath\n });\n\n // add documentation page\n if (settings.documentationPage === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: settings.documentationPath,\n config: {\n auth: settings.auth\n },\n handler: (request, reply) => {\n\n reply.view('index.html', {});\n }\n }]);\n }\n\n // add swagger UI if asked for or need by documentation page\n if (settings.documentationPage === true || settings.swaggerUI === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: settings.swaggerUIPath + '{path*}',\n config: {\n auth: false\n },\n handler: {\n directory: {\n path: swaggerDirPath + Path.sep,\n listing: true,\n index: false\n }\n }\n }, {\n method: 'GET',\n path: settings.swaggerUIPath + 'extend.js',\n config: {\n auth: false,\n files: {\n relativeTo: publicDirPath\n }\n },\n handler: {\n file: 'extend.js'\n }\n }]);\n }\n\n // add debug page\n if (settings.debug === true) {\n pluginWithDependencies.route([{\n method: 'GET',\n path: Path.join(settings.documentationPath, Path.sep, 'debug').split(Path.sep).join('/'),\n config: {\n auth: settings.auth\n },\n handler: (request, reply) => {\n\n reply.view('debug.html', {}).type('application/json');\n }\n }]);\n }\n\n appendDataContext(pluginWithDependencies, settings);\n\n nextWithDependencies();\n\n });\n }\n\n // TODO: need to work how to test this as it need a request object\n // Undocument API interface, it may change\n /* $lab:coverage:off$ */\n plugin.expose('getJSON', function (exposeOptions, request, callback) {\n\n // use either options passed to function or plug-in scope options\n let exposeSettings = {};\n if (exposeOptions && Utilities.hasProperties(exposeOptions)) {\n exposeSettings = Hoek.applyToDefaults(Defaults, exposeOptions);\n Joi.assert(exposeSettings, schema);\n } else {\n exposeSettings = Hoek.clone(settings);\n }\n Builder.getSwaggerJSON(exposeSettings, request, callback);\n });\n /* $lab:coverage:on$ */\n\n\n next();\n},
[41m"name"[0m[31m [1]: -- missing --[0m
}
}
[31m
[1] "name" is required[0m
at Object.exports.apply (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/config.js:22:10)
at internals.Server.register (/Users/karthik/Desktop/new_gem/es6/CriApi/node_modules/hapi/lib/server.js:364:31)
at liftOff (/Users/karthik/Desktop/new_gem/es6/CriApi/server.js:30:18)
at Object.<anonymous> (/Users/karthik/Desktop/new_gem/es6/CriApi/server.js:36:1)
at Module._compile (module.js:632:14)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
server.register({
plugin: require('Plugin_name'),
options:{
'valid_Options here'
}}).then( () => {server.start()})