Thanks to visit codestin.com
Credit goes to github.com

Skip to content

danilovaz/hapi

 
 

Repository files navigation

Web and services application framework

Lead Maintainer: Eran Hammer

hapi is a simple to use configuration-centric framework with built-in support for input validation, caching, authentication, and other essential facilities for building web and services applications. hapi enables developers to focus on writing reusable application in a highly modular and prescriptive approach.

For the latest updates and release information visit hapijs.com and follow @hapijs on twitter.

Getting Started

Start by creating a package.json:

$ npm init

Install hapi and save it to your package.json dependencies:

$ npm install hapi --save

Create an index.js file with the following contents:

var Hapi = require('hapi');

// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
  host: 'localhost',
  port: 8000
});

// Add the route
server.route({
  method: 'GET',
  path:'/hello',
  handler: function (request, reply) {
    reply('hello world');
  }
});

// Start the server
server.start();

Launch the application by running node . and open localhost:8000/hello in your browser.

For more example usage check out tutorials

Using plugins

A common desire when creating any web application, is an access log. To add some basic logging to our application, let's load the good plugin and its good-console reporter on to our server.

The plugin first needs to be installed:

$ npm install --save good
$ npm install --save good-console

Then update your server.js:

var Hapi = require('hapi');
var Good = require('good');

var server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    reply('Hello, world!');
  }
});

server.route({
  method: 'GET',
  path: '/{name}',
  handler: function (request, reply) {
    reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
  }
});

server.register({
  register: Good,
  options: {
    reporters: [{
      reporter: require('good-console'),
      args:[{ log: '*', response: '*' }]
    }]
  }
}, function (err) {
    if (err) {
      throw err; // something bad happened loading the plugin
    }

    server.start(function () {
      server.log('info', 'Server running at: ' + server.info.uri);
    });
});

Now when the server is started you'll see:

140625/143008.751, info, Server running at: http://localhost:3000

And if we visit http://localhost:3000/ in the browser, you'll see:

140625/143205.774, request, http://localhost:3000: get / {} 200 (10ms)

This is just one short example of what plugins are capable of, for more information check out the plugins tutorial

Version

Development version: 8.1.x (release notes) Build Status

Support

If you have questions, please open an issue in the discussion forum.

About

Server Framework for Node.js

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.9%
  • Shell 0.1%