Last active
December 30, 2017 00:00
-
-
Save ivanvs/9602cedac77baeec1d5de7aeb9ee274c to your computer and use it in GitHub Desktop.
Example of using hapi-swagger on hapi.js ^17.0.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Hapi = require('hapi'); | |
const Inert = require('inert'); | |
const Vision = require('vision'); | |
const HapiSwagger = require('hapi-swagger'); | |
const Pack = require('./package'); | |
const server = Hapi.Server({ | |
host: 'localhost', | |
port: 3000 | |
}); | |
async function registerRoutes() { | |
await server.route({ | |
method: 'GET', | |
path: '/hello', | |
config: { | |
description: 'Get hello world message', | |
notes: 'Get hello world message', | |
tags: ['api'], | |
handler: function (request, h) { | |
return 'hello world'; | |
} | |
} | |
}); | |
} | |
async function setupSwagger() { | |
const options = { | |
info: { | |
'title': 'Test API Documentation', | |
'version': Pack.version, | |
} | |
}; | |
await server.register([ | |
Inert, | |
Vision, | |
{ | |
plugin: HapiSwagger, | |
options: options | |
} | |
]); | |
} | |
async function start() { | |
try { | |
await setupSwagger(); | |
await registerRoutes(); | |
await server.start(); | |
console.log('Server running at:', server.info.uri); | |
} catch (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment