Created
September 4, 2017 10:12
-
-
Save pankajpatel/0dcce9fc11b2364b7e1618ddab8fb7f4 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const Hapi = require('hapi'); | |
const server = new Hapi.Server(); | |
server.connection({ | |
host: 'localhost', | |
port: Number(process.argv[2]) || 8080 | |
}); | |
// Add the route | |
server.route({ | |
method: 'GET', | |
path: '/', | |
handler: function (request, reply) { | |
return reply('HapiJS Server running!'); | |
} | |
}); | |
server.register({ | |
register: require('hapi-response-time'), | |
options: { | |
route: { | |
name: 'health', | |
methods: ['POST'], | |
message: 'Hello from MyAwesomePlugin' | |
} | |
} | |
}, err => { | |
if (err) throw err; | |
}); | |
// Start the server | |
server.start((err) => { | |
if (err) { | |
throw err; | |
} | |
console.log('Server running at:', server.info.uri); | |
}); |
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 Plugin = {}; | |
Plugin.register = (server, options, next) => { | |
// Do some stuff with server | |
server.register(require('hapi-response-time'), (err) => { | |
if (err) throw err; | |
}); | |
server.route([{ | |
method: ['GET', 'POST'], | |
path: '/ping', | |
handler: (req, reply) => { | |
reply('Hello world!'); | |
} | |
}]); | |
// Let hapijs know that you are done and move to next plugin | |
next(); | |
}; | |
Plugin.register.attributes = { | |
name: 'MyAwesomePlugin', | |
version: '0.1.0' | |
}; | |
module.exports = Plugin; |
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
'use strict'; | |
const Hapi = require('hapi'); | |
const server = new Hapi.Server(); | |
server.connection({ | |
host: 'localhost', | |
port: Number(process.argv[2]) || 8080 | |
}); | |
// Add the route | |
server.route({ | |
method: 'GET', | |
path: '/', | |
handler: function (request, reply) { | |
return reply('HapiJS Server running!'); | |
} | |
}); | |
server.register(require('hapi-response-time'), err => { | |
if (err) throw err; | |
}); | |
// Start the server | |
server.start((err) => { | |
if (err) { | |
throw err; | |
} | |
console.log('Server running at:', server.info.uri); | |
}); |
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 Plugin = {}; | |
Plugin.register = (server, options, next) => { | |
server.register(require('hapi-response-time'), (err) => { | |
if (err) throw err; | |
}); | |
if (options.route) { | |
server.route([{ | |
method: options.route.methods || ['GET', 'POST'], | |
path: `/${options.route.name || 'ping'}`, | |
handler: (req, reply) => { | |
reply(options.route.message || 'Hello world!'); | |
} | |
}]); | |
} | |
next(); | |
}; | |
Plugin.register.attributes = { | |
name: 'MyAwesomePlugin', | |
version: '0.1.0' | |
}; | |
module.exports = Plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment