This section provides an overview of what hapijs is, and why a developer might want to use it.
It should also mention any large subjects within hapijs, and link out to the related topics. Since the Documentation for hapijs is new, you may need to create initial versions of those related topics.
Version | Release Notes | Release Date |
---|---|---|
16 | https://github.com/hapijs/hapi/issues/3398 | 2016-11-29 |
15 | https://github.com/hapijs/hapi/issues/3323 | 2016-08-26 |
14 | https://github.com/hapijs/hapi/issues/3272 | 2016-07-29 |
13 | https://github.com/hapijs/hapi/issues/3040 | 2016-02-01 |
12 | https://github.com/hapijs/hapi/issues/2985 | 2016-01-04 |
11 | https://github.com/hapijs/hapi/issues/2850 | 2015-10-16 |
10 | https://github.com/hapijs/hapi/issues/2764 | 2015-09-11 |
server.js
file with the following contents:'use strict';
const Hapi = require('hapi');
// Create a server instance
const server = new Hapi.Server();
// Specify connections (server available on http://localhost:8000)
server.connection({
port: 8000
});
// Add a route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
return reply('hello world');
}
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
Run node server.js
and open http://localhost:8000/hello in your browser.
Parameters can be specified in path
property of route configuration
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
// Add a route path with url param
server.route({
method: 'GET',
path:'/hello/{name}',
handler: function (request, reply) {
// Passed parameter is accessible via "request.params"
return reply(`Hello ${request.params.name}`);
}
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
'use strict';
const Hapi = require('hapi');
const Joi = require('joi');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
/**
* Add a route path with url param
*/
server.route({
method: 'GET',
path:'/hello/{name}',
handler: function (request, reply) {
// Passed parameter is accessible via "request.params"
return reply(`Hello ${request.params.name}`);
},
config: {
// Validate the {name} url param
validate: {
params: Joi.string().required()
}
}
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});