Hapi.Server
new Server([host], [port], [options])
createServer([host], [port], [options])
- Server options
Server
propertiesServer
methodsserver.start([callback])
server.stop([options], [callback])
server.route(options)
server.route(routes)
server.routingTable()
server.log(tags, [data, [timestamp]])
server.state(name, [options])
server.views(options)
server.auth(name, options)
server.ext(event, method, [options])
server.helper(name, method, [options])
server.inject(options, callback)
Server
events
- Request object
request
propertiesrequest
methods
Hapi.response
Hapi.error
- Error transformation -badRequest([message])
-unauthorized(message, [scheme, [attributes]])
-unauthorized(message, wwwAuthenticate)
-clientTimeout([message])
-serverTimeout([message])
-forbidden([message])
-notFound([message])
-internal([message, [data]])
-passThrough(code, payload, contentType, headers)
Hapi.Pack
-new Pack([options])
-Pack
properties -Pack
methods -pack.server([host], [port], [options])
-pack.start([callback])
-pack.stop([options], [callback])
-pack.allow(permissions)
-pack.require(name, options, callback)
-pack.require(names, callback)
-pack.register(plugin, options, callback)
Hapi.Composer
-new Composer(manifest)
-composer.compose(callback)
-composer.start([callback])
-composer.stop([options], [callback])
- Plugin interface
Hapi.utils
-version()
Hapi.types
Hapi.state
-prepareValue(name, value, options, callback)
Creates a new server instance with the following arguments:
host
- the hostname or IP address the server is bound to. Defaults to0.0.0.0
which means any available network interface. Set to127.0.0.1
orlocalhost
to restrict connection to those coming from the same machine.port
- the TPC port the server is listening to. Defaults to port80
for HTTP and to443
when TLS is configured. to use an ephemeral port, use0
and once the server is started, retrieve the port allocation viaserver.info.port
.options
- An object with the server configuration as described in server options.
var Hapi = require('hapi');
var server = new Hapi.Server('localhost', 8000, { cors: true });
An alternative method for creating a server instance using the same arguments as new Server()
.
var Hapi = require('hapi');
var server = Hapi.createServer('localhost', 8000, { cors: true });
When creating a server instance, the following options configure the server's behavior:
app
- application-specific configuration. Provides a safe place to store application configuration without potential conflicts with hapi. Should not be used by plugins which should useplugins[name]
.
Each instance of the Server
object have the following properties:
app
- application-specific state. Provides a safe place to store application data without potential conflicts with hapi. Should not be used by plugins which should useplugins[name]
.helpers
- helper functions registered withserver.helper()
.info
- server information:port
- the port the server was configured to (beforestart()
) or bound to (afterstart()
).host
- the hostname the server was configured to (defaults to'0.0.0.0'
if no host was provided).protocol
- the protocol used (e.g.'http'
or'https'
).uri
- a string with the following format: 'protocol://host:port' (e.g. 'http://example.com:8080').
listener
- the node HTTP server object.pack
- thePack
object the server belongs to (automatically assigned when creating a server instance directly).plugins
- an object where each key is a plugin name and the value is the API registered by that plugin usingplugin.api()
.settings
- an object containing the server options after applying the defaults.
Starts the server by listening for incoming connections on the configured port. If provided, callback()
is called once the server is
ready for new connections. If the server is already started, the callback()
is called on the next tick.
var Hapi = require('hapi');
var server = new Hapi();
server.start(function () {
console.log('Server started at: ' + server.info.uri);
});
Stops the server by refusing to accept any new connections. Existing connections will continue until closed or timeout (defaults to 5 seconds).
Once the server stopped, all the connections have ended, and it is safe to exit the process, the callback (if provided) is called. If the server
is already stopped, the callback()
is called on the next tick.
The optional options
object supports:
timeout
- overrides the timeout in millisecond before forcefully terminating a connection. Defaults to5000
(5 seconds).
server.stop({ timeout: 60 * 1000 }, function () {
console.log('Server stopped');
});
Adds a new route to the server where:
options
- the route configuration as described in route options.
The following options are available when adding a route:
path
- (required) the absolute path used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the serverrouter
configuration option. The path can include named parameters enclosed in{}
which will be matched against literal values in the request as described in Path parameters.
var Hapi = require('hapi');
var server = new Hapi.Server();
// Handler in top level
var status = function () {
this.reply('ok');
};
server.route({ method: 'GET', path: '/status', handler: status });
// Handler in config
var user = {
cache: { expiresIn: 5000 },
handler: function () {
this.reply({ name: 'John' });
}
};
server.route({ method: 'GET', path: '/user', config: user });
The router iterates through the routing table on each incoming request and executes the first (and only the first) matching route. Route matching is done on the request path only (excluding the query and other URI components). Requests are matches in a deterministic order where the order in which routes are added does not matter. The routes are sorted from the most specific to the most generic. For example, the following path array shows the order in which an incoming request path will be matched against the routes:
var paths = [
'/',
'/a',
'/b',
'/ab',
'/{p}',
'/a/b',
'/a/{p}',
'/b/',
'/a/b/c',
'/a/b/{p}',
'/a/{p}/b',
'/a/{p}/c',
'/a/{p*2}',
'/a/b/c/d',
'/a/b/{p*2}',
'/a/{p}/b/{x}',
'/{p*5}',
'/a/b/{p*}',
'/{p*}'
];
Parameterized paths are processed by matching the named parameters to the content of the incoming request path at that path segment. For example,
'/book/{id}/cover' will match '/book/123/cover' and request.params.id
will be set to '123'
. Each path segment (everything between the opening '/' and
the closing '/' unless it is the end of the path) can only include one named parameter.
An optional '?' suffix following the parameter name indicates an optional parameter (only allowed if the parameter is at the ends of the path). For example, the route '/book/{id?}' matches '/book/'.
var getAlbum = function () {
this.reply('You asked for ' +
(this.params.song ? this.params.song + ' from ' : '') +
this.params.album);
};
server.route({
path: '/{album}/{song?}',
method: 'GET',
handler: getAlbum
});
In addition to the optional '?' suffix, a parameter name can also specify the number of matching segments using the '' suffix, followed by a number greater than 1. If the number of expected parts can be anything, then use '' without a number (matching any number of segments can only be used in the last path segment).
var getPerson = function () {
var nameParts = this.params.name.split('/');
this.reply({ first: nameParts[0], last: nameParts[1] });
};
server.route({
path: '/person/{name*2}', // Matches '/person/john/doe'
method: 'GET',
handler: getPerson
});
When a route is matched against an incoming request, the route handler is called and passed a reference to the request object.
The handler method must call request.reply()
or one of its sub-methods to return control back to the router.
Route handler functions can use one of three declaration styles:
No arguments (the request
object is bound to this
, decorated by the reply
interface):
var handler = function () {
this.reply('success');
};
One argument (the request is passed as an argument, decorated by the reply
interface):
var handler = function (request) {
request.reply('success');
};
Two arguments (the request and the reply
interface are passed as arguments):
var handler = function (request, reply) {
reply('success');
};
The two-arguments style is provided for symmetry with extension functions and prerequisite functions where the function
signature is function(request, next)
. In order to enable interchangeable use of these functions, the two argument style does
not provide any of the request.reply
decorations.
It is often necessary to perform prerequisite actions before the handler is called (e.g. load required reference data from a database).
The route pre
option allows defining such pre-handler methods. The methods are called in order, unless a mode
is specified with value
'parallel'
in which case, all the parallel methods are executed first, then the rest in order. pre
can be assigned a mixed array of:
- objects with:
method
- the function to call (or short-hand helper string as described below). The function signature isfunction(request, next)
where:request
- the incomingrequest
object.next
- the function called when the method is done with the signaturefunction(result)
where:result
- any return value including anError
object (created vianew Error()
orHapi.error
). If an error is returned, that value is sent back to the client and the handler method is not called.
assign
- key name to assign the result of the function to withinrequest.pre
.mode
- set the calling order of the function. Available values:'serial'
- serial methods are executed after all the'parallel'
methods in the order listed. This is the default value.'parallel'
- all parallel methods are executed first in parallel before any serial method. The first to return an error response will exist the set.
- functions - same as including an object with a single
method
key. - strings - special short-hand notation for registered server helpers using the format 'name(args)'
(e.g.
'user(params.id)'
) where:- 'name' - the helper name. The name is also used as the default value of
assign
. - 'args' - the helper arguments (excluding
next
) where each argument is a property ofrequest
.
- 'name' - the helper name. The name is also used as the default value of
var Hapi = require('hapi');
var server = new Hapi.Server();
var pre1 = function (request, next) {
next('Hello');
};
var pre2 = function (request, next) {
next('World');
};
var pre3 = function (request, next) {
next(request.pre.m1 + ' ' + request.pre.m2);
};
server.route({
method: 'GET',
path: '/',
config: {
pre: [
{ method: pre1, assign: 'm1', mode: 'parallel' },
{ method: pre2, assign: 'm2', mode: 'parallel' },
{ method: pre3, assign: 'm3' },
],
handler: function () {
this.reply(this.pre.m3 + '\n');
}
}
});
If the application needs to override the default Not Found (404) error response, it can add a catch-all route for a specific method or all methods. Only one catch-all route can be defined per server instance.
var Hapi = require('hapi');
var server = new Hapi.Server();
var handler = function () {
this.reply('The page was not found').code(404);
};
server.route({ method: '*', path: '/{p*}', handler: handler });
Same as server.route(options) where routes
is an array of route options.
server.route([
{ method: 'GET', path: '/status', handler: status },
{ method: 'GET', path: '/user', config: user }
]);
Returns a copy of the routing table. The return value is an array of routes where each route contains:
settings
- the route config with defaults applied.method
- the HTTP method in lower case.path
- the route path.
var table = server.routingTable()
console.log(table);
/* Output:
[{
method: 'get',
path: '/test/{p}/end',
settings: {
handler: [Function],
method: 'get',
plugins: {},
app: {},
validate: {},
payload: 'stream',
auth: undefined,
cache: [Object] }
}] */
The server.log()
method is used for logging server events that cannot be associated with a specific request. When called the server emits a 'log'
event which can be used by other listeners or plugins to record the information or output to the console. The arguments are:
tags
- a string or an array of strings (e.g.['error', 'database', 'read']
) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the'hapi'
tag along with event-specific information.data
- an optional message string or object with the application data being logged.timestamp
- an optional timestamp expressed in milliseconds. Defaults toDate.now()
(now).
var Hapi = require('hapi');
var server = new Hapi.Server();
server.on('log', function (event, tags) {
if (tags.error) {
console.log(event);
}
});
server.log(['test', 'error'], 'Test event');
HTTP state management uses client cookies to persist a state across multiple requests. Cookie definitions
can be registered with the server using the server.state()
method, where:
name
- is the cookie name.options
- are the optional cookie settings:ttl
- time-to-live in milliseconds. Defaults tonull
(session time-life - cookies are deleted when the browser is closed).isSecure
- sets the 'Secure' flag. Defaults tofalse
.isHttpOnly
- sets the 'HttpOnly' flag. Defaults tofalse
.path
- the path scope. Defaults tonull
(no path).domain
- the domain scope. Defaults tonull
(no domain).autoValue
- if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value.encoding
- encoding performs on the provided value before serialization. Options are:'none'
- no encoding. When used, the cookie value must be a string. This is the default value.'base64'
- string value is encoded using Base64.'base64json'
- object value is JSON-stringified than encoded using Base64.'form'
- object value is encoded using the x-www-form-urlencoded method.'iron'
- Encrypts and sign the value using iron.
sign
- an object used to calculate an HMAC for cookie integrity validation. This does not provide privacy, only a mean to verify that the cookie value was generated by the server. Redundant when'iron'
encoding is used. Options are:integrity
- algorithm options. Defaults torequire('iron').defaults.integrity
.password
- password used for HMAC key generation.
password
- password used for'iron'
encoding.iron
- options for'iron'
encoding. Defaults torequire('iron').defaults
.
// Set cookie definition
server.state('session', {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: true,
path: '/',
encoding: 'base64json'
});
// Set state in route handler
var handler = function () {
var session = this.state.session;
if (!session) {
session = { user: 'joe' };
}
session.last = Date.now();
this.reply('Success').state('session', session);
};
Registered cookies are automatically parsed when received. Parsing rules depends on the server state.cookies
configuration.
If an incoming registered cookie fails parsing, it is not included in request.state
, regardless of the state.cookies.failAction
setting.
When state.cookies.failAction
is set to 'log'
and an invalid cookie value is received, the server will emit a 'request'
event. To capture these errors
subscribe to the 'request'
events and filter on 'error'
and 'state'
tags:
server.on('request', function (request, event, tags) {
if (tags.error && tags.state) {
console.error(event);
}
});
Initializes the server views manager programmatically instead of via the server views
configuration option.
The options
object is the same as the server views
config object.
server.views({
engines: {
html: 'handlebars',
jade: 'jade'
},
path: '/static/templates'
});
Registers an authentication strategy where:
name
- is the strategy name ('default'
is automatically assigned if a single strategy is registered via the serverauth
config).options
- required strategy options. Each scheme comes with its own set of required options, in addition to the options shared by all schemes:scheme
- (required, except whenimplementation
is used) the built-in scheme name. Available values:'basic'
- HTTP Basic authentication (RFC 2617)'cookie'
- cookie authentication'hawk'
- HTTP Hawk authentication (Hawk protocol)'bewit'
- URI Bewit (Hawk) query authentication (Hawk protocol)
implementation
- an object with the hapi authentication scheme interface (use the'hawk'
implementation as template). Cannot be used together withscheme
.defaultMode
- iftrue
, the scheme is automatically assigned as a required strategy to any route without anauth
config. Can only be assigned to a single server strategy. Value must betrue
(which is the same as'required'
) or a valid authentication mode ('required'
,'optional'
,'try'
). Defaults tofalse
.
Basic authentication requires validating a username and password combination. The 'basic'
scheme takes the following required options:
scheme
- set to'basic'
.validateFunc
- a user lookup and password validation function with the signaturefunction(username, password, callback)
where:username
- the username received from the client.password
- the password received from the client.callback
- a callback function with the signaturefunction(err, isValid, credentials)
where:err
- an internal error.isValid
-true
if both the username was found and the password matched, otherwisefalse
.credentials
- a credentials object passed back to the application inrequest.auth.credentials
. Typically,credentials
are only included whenisValid
istrue
, but there are cases when the application needs to know who tried to authenticate even when it fails (e.g. with authentication mode'try'
).
var Bcrypt = require('bcrypt');
var users = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a'
}
};
var validate = function (username, password, callback) {
var user = users[username];
if (!user) {
return callback(null, false);
}
Bcrypt.compare(password, user.password, function (err, isValid) {
callback(err, isValid, { id: user.id, name: user.name });
});
};
server.auth('simple', {
scheme: 'basic',
validateFunc: validate
});
server.route({ method: 'GET', path: '/', config: { auth: 'simple' } });
Cookie authentication provides a simple cookie-based session management. The user has to be authenticated via other means, typically a web
form, and upon successful authentication, receive a reply with a session cookie. Subsequent requests containing the session cookie are authenticated
(the cookie uses Iron to encrypt and sign the session content) and validated via the provided validateFunc
in case the cookie's encrypted content requires validation on each request. Note that cookie operates as a bearer token and anyone in possession
of the cookie content can use it to impersonate its true owner. The 'cookie
' scheme takes the following required options:
scheme
- set to'cookie'
.cookie
- the cookie name. Defaults to'sid'
.password
- used for Iron cookie encoding.ttl
- sets the cookie expires time in milliseconds. Defaults to single browser session (ends when browser closes).clearInvalid
- iftrue
, any authentication cookie that fails validation will be marked as expired in the response and cleared. Defaults tofalse
.isSecure
- iffalse
, the cookie is allowed to be transmitted over insecure connections which exposes it to attacks. Defaults totrue
.redirectTo
- optional login URI to redirect unauthenticated requests to. Defaults to no redirection.appendNext
- iftrue
andredirectTo
istrue
, appends the current request path to the query component of theredirectTo
URI using the parameter name'next'
. Set to a string to use a different parameter name. Defaults tofalse
.validateFunc
- an optional session validation function used to validate the content of the session cookie on each request. Used to verify that the internal session state is still valid (e.g. user account still exists). The function has the signaturefunction(session, callback)
where:session
- is the session object set viarequest.auth.session.set()
.callback
- a callback function with the signaturefunction(err, isValid, credentials)
where:err
- an internal error.isValid
-true
if the content of the session is valid, otherwisefalse
.credentials
- a credentials object passed back to the application inrequest.auth.credentials
. If value isnull
orundefined
, defaults tosession
. If set, will override the current cookie as ifrequest.auth.session.set()
was called.
When the cookie scheme is enabled on a route, the request.auth.session
objects is decorated with two methods:
set(session)
- sets the current session. Must be called after a successful login to begin the session.session
must be a non-null object, which is set on successful subsequent authentications inrequest.auth.credentials
.clear()
- clears the current session. Used to logout a user.
var Hapi = require('hapi');
var users = {
john: {
id: 'john',
password: 'password',
name: 'John Doe'
}
};
var home = function () {
this.reply('<html><head><title>Login page</title></head><body><h3>Welcome '
+ this.auth.credentials.name
+ '!</h3><br/><form method="get" action="/logout">'
+ '<input type="submit" value="Logout">'
+ '</form></body></html>');
};
var login = function () {
if (this.auth.isAuthenticated) {
return this.reply.redirect('/');
}
var message = '';
var account = null;
if (this.method === 'post') {
if (!this.payload.username ||
!this.payload.password) {
message = 'Missing username or password';
}
else {
account = users[this.payload.username];
if (!account ||
account.password !== this.payload.password) {
message = 'Invalid username or password';
}
}
}
if (this.method === 'get' ||
message) {
return this.reply('<html><head><title>Login page</title></head><body>'
+ (message ? '<h3>' + message + '</h3><br/>' : '')
+ '<form method="post" action="/login">'
+ 'Username: <input type="text" name="username"><br>'
+ 'Password: <input type="password" name="password"><br/>'
+ '<input type="submit" value="Login"></form></body></html>');
}
this.auth.session.set(account);
return this.reply.redirect('/');
};
var logout = function () {
this.auth.session.clear();
return this.reply.redirect('/');
};
var server = new Hapi.Server('localhost', 8000, config);
server.auth('session', {
scheme: 'cookie',
password: 'secret',
cookie: 'sid-example',
redirectTo: '/login'
});
server.route([
{ method: 'GET', path: '/', config: { handler: home, auth: true } },
{ method: '*', path: '/login', config: { handler: login, auth: { mode: 'try' } } },
{ method: 'GET', path: '/logout', config: { handler: logout, auth: true } }
]);
server.start();
Hawk authentication provides a holder-of-key authentication scheme. The scheme supports payload authentication. The scheme requires the following options:
scheme
- set to'hawk'
.getCredentialsFunc
- credential lookup function with the signaturefunction(id, callback)
where:id
- the Hawk credentials identifier.callback
- the callback function with signaturefunction(err, credentials)
where:err
- an internal error.credentials
- a credentials object passed back to the application inrequest.auth.credentials
. Returnnull
orundefined
to indicate unknown credentials (which is not considered an error state).
hostHeaderName
- optional name of the HTTP request header used to transmit host information. Defaults to ''host''.
var Hapi = require('hapi');
var server = new Hapi.Server(config);
var credentials = {
d74s3nz2873n: {
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
}
var getCredentials = function (id, callback) {
return callback(null, credentials[id]);
};
server.auth('hawk', {
scheme: 'hawk',
getCredentialsFunc: getCredentials
});
Bewit authentication provides a short-term access to a protected resource by including a token (bewit) in the request query, issued by an authorized party. Bewit is a subset of the Hawk protocol. The scheme can only be used with 'GET' requests and requires the following options:
scheme
- set to'bewit'
.getCredentialsFunc
- credential lookup function with the signaturefunction(id, callback)
where:id
- the Hawk credentials identifier.callback
- the callback function with signaturefunction(err, credentials)
where:err
- an internal error.credentials
- a credentials object passed back to the application inrequest.auth.credentials
. Returnnull
orundefined
to indicate unknown credentials (which is not considered an error state).
hostHeaderName
- optional name of the HTTP request header used to transmit host information. Defaults to ''host''.
var Hapi = require('hapi');
var server = new Hapi.Server(config);
var credentials = {
d74s3nz2873n: {
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
}
var getCredentials = function (id, callback) {
return callback(null, credentials[id]);
};
server.auth('bewit', {
scheme: 'bewit',
getCredentialsFunc: getCredentials
});
To send an authenticated Bewit request, the URI must contain the 'bewit'
query parameter which can be generated using the Hawk module:
var Hawk = require('hawk');
var credentials = {
id: 'd74s3nz2873n',
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
};
var uri = 'http://example.com:8080/endpoint';
var bewit = Hawk.client.getBewit(uri, { credentials: credentials, ttlSec: 60 });
uri += '?bewit=' + bewit;
Registers an extension function in one of the available extension points where:
event
- the event name.method
- a function or an array of functions to be executed at a specified point during request processing. The required extension function signature isfunction(request, next)
where:request
- the incomingrequest
object.next
- the callback function the extension method must call to return control over to the router with signaturefunction(exit)
where:exit
- optional request processing exit response. If set to a non-falsy value, the request lifecycle process will jump to the "send response" step, skipping all other steps in between, and using theexit
value as the new response.exit
can be any result value accepted byrequest.reply()
.
options
- an optional object with the following:before
- a string or array of strings of plugin names this method must executed before (on the same event). Otherwise, extension methods are executed in the order added.after
- a string or array of strings of plugin names this method must executed before (on the same event). Otherwise, extension methods are executed in the order added.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.ext('onRequest', function (request, next) {
// Change all requests to '/test'
request.setUrl('/test');
next();
});
var handler = function () {
this.reply({ status: 'ok' });
};
server.route({ method: 'GET', path: '/test', handler: handler });
server.start();
// All requests will get routed to '/test'
Each incoming request passes through a pre-defined set of steps, along with optional extensions:
'onRequest'
extension point- always called
- the
request
object passed to the extension functions is decorated with therequest.setUrl(url)
andrequest.setMethod(verb)
methods. Calls to these methods will impact how the request is routed and can be used for rewrite rules.
- Lookup route using request path
- Parse cookies
'onPreAuth'
extension point- Authenticate request
- Read and parse payload
- Authenticate request payload
'onPostAuth'
extension point- Validate path parameters
- Process query extensions (e.g. JSONP)
- Validate query
- Validate payload
'onPreHandler'
extension point- Route prerequisites
- Route handler
'onPostHandler'
extension point- the
request
object passed to the extension function is decorated with therequest.response()
method which returns the response object. The response object may be modified. To return a different response (for example, replace an error with an HTML response), return the new response vianext(response)
.
- the
- Validate response payload
'onPreResponse'
extension point- always called
- the
request
object passed to the extension function is decorated with therequest.response()
method which returns the response object. The response object may be modified. To return a different response (for example, replace an error with an HTML response), return the new response vianext(response)
.
- Send response (may emit
'internalError'
event) - Emits
'response'
event - Wait for tails
- Emits
'tail'
event
Registers a server helper function. Server helpers are functions registered with the server and used throughout the application as a common utility. Their advantage is in the ability to configure them to use the built-in cache and shared across multiple request handlers without having to create a common module.
Helpers are registered via server.helper(name, method, [options])
where:
name
- a unique helper name used to invoke the method viaserver.helpers[name]
.method
- the helper function with the signature isfunction(arg1, arg2, ..., argn, next)
where:arg1
,arg2
, etc. - the helper function arguments.next
- the function called when the helper is done with the signaturefunction(result)
where:result
- any return value including anError
object (created vianew Error()
orHapi.error
).
options
- optional configuration:cache
- cache configuration as described in catbox module documentation:expiresIn
- relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together withexpiresAt
.expiresAt
- time of day expressed in 24h notation using the 'MM:HH' format, at which point all cache records for the route expire. Cannot be used together withexpiresIn
.staleIn
- number of milliseconds to mark an item stored in cache as stale and reload it. Must be less thanexpiresIn
.staleTimeout
- number of milliseconds to wait before checking if an item is stale.segment
- optional segment name, used to isolate cached items within the cache partition. Defaults to '#name' where 'name' is the helper name. When setting segment manually, it must begin with '##'.
generateKey
- a function used to generate a unique key (for caching) from the arguments passed to the helper function (with the exception of the last 'next' argument). The server will automatically generate a unique key if the function's arguments are all of types'string'
,'number'
, or'boolean'
. However if the helper uses other types of arguments, a key generation function must be provided which takes the same arguments as the function and returns a unique string (ornull
if no key can be generated). Note that when thegenerateKey
method is invoked, the arguments list will include thenext
argument which must not be used in calculation of the key.
var Hapi = require('hapi');
var server = new Hapi.Server();
// Simple arguments
var add = function (a, b, next) {
next(a + b);
};
server.helper('sum', add, { cache: { expiresIn: 2000 } });
server.helpers.sum(4, 5, function (result) {
console.log(result);
});
// Object argument
var addArray = function (array, next) {
var sum = 0;
array.forEach(function (item) {
sum += item;
});
next(sum);
};
server.helper('sumObj', addArray, {
cache: { expiresIn: 2000 },
generateKey: function (array) {
return array.join(',');
}
});
server.helpers.sumObj([5, 6], function (result) {
console.log(result);
});
Injects a request into the server simulating an incoming HTTP request without making an actual socket connection. Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack. Utilizes the shot module for performing injections, with some additional options and response properties:
options
- can be assign a string with the requested URI, or an object with:method
- the request HTTP method (e.g.'POST'
). Defaults to'GET'
.url
- the request URL. If the URI includes an authority (e.g.'example.com:8080'
), it is used to automatically set an HTTP 'Host' header, unless one was specified inheaders
.headers
- an object with optional request headers where each key is the header name and the value is the header content. Defaults to no additions to the default Shot headers.payload
- an optional string or buffer containing the request payload. Defaults to no payload.credentials
- an optional credentials object containing authentication information. Thecredentials
are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Defaults to no credentials.simulate
- an object with options used to simulate client request stream conditions for testing:error
- iftrue
, emits an'error'
event after payload transmission (if any). Defaults tofalse
.close
- iftrue
, emits a'close'
event after payload transmission (if any). Defaults tofalse
.end
- iffalse
, does not end the stream. Defaults totrue
.
callback
- the callback function with signaturefunction(res)
where:res
- the response object where:statusCode
- the HTTP status code.headers
- an array containing the headers set.payload
- the response payload string.raw
- an object with the injection request and response objects:req
- therequest
object.res
- the response object.
result
- the raw handler response (e.g. when not a stream) before it is serialized for transmission. If not available, set topayload
. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string).
var Hapi = require('hapi');
var server = new Hapi.Server();
var get = function () {
this.reply('Success!');
};
server.route({ method: 'GET', path: '/', handler: get });
server.inject('/', function (res) {
console.log(res.result);
});
The server object inherits from Events.EventEmitter
and emits the following events:
'log'
- events logged with server.log().'request'
- events generated by request.log() or internally (multiple events per request).'response'
- emitted after a response to a client request is sent back. Single event per request.'tail'
- emitted when a request finished processing, including any registered tails. Single event per request.'internalError'
- emitted whenever an Internal Server Error (500) error response is sent. Single event per request.
When provided (as listed below) the event
object include:
timestamp
- the event timestamp.id
- if the event relates to a request, the request id.tags
- an array of tags (e.g.['error', 'http']
). Includes the'hapi'
tag is the event was generated internally.data
- optional event-specific information.
The 'log'
event includes the event
object and a tags
object (where each tag is a key with the value true
):
server.on('log', function (event, tags) {
if (tags.error) {
console.log('Server error: ' + (event.data || 'unspecified'));
}
});
The 'request'
event includes the request
object, the event
object, and a tags
object (where each tag is a key with the value true
):
server.on('request', function (request, event, tags) {
if (tags.received) {
console.log('New request: ' + event.id);
}
});
The 'response'
and 'tail'
events include the request
object:
server.on('response', function (request) {
console.log('Response sent for request: ' + request.id);
});
The 'internalError'
event includes the request
object and the causing error err
object:
server.on('internalError', function (request, err) {
console.log('Error response (500) sent for request: ' + request.id + ' because: ' + err.message);
});
The request
object is created internally for each incoming request. It is not the node request
object received from the HTTP
server callback (which is available in request.raw.req
). The request
object methods and properties change through the
request lifecycle.
Each request object have the following properties:
app
- application-specific state. Provides a safe place to store application data without potential conflicts with hapi. Should not be used by plugins which should useplugins[name]
.auth
- authentication information:isAuthenticated
-true
is the request has been successfully authenticated, otherwisefalse
.credentials
- thecredential
object received during the authentication process. The presence of an object does not mean successful authentication.artifacts
- an artifact object received from the authentication strategy and used in authentication-related actions.session
- an object used by the'cookie'
authentication scheme.
id
- a unique request identifier.info
- request information:received
- request reception timestamp.address
- remote client IP address.referrer
- content of the HTTP 'Referrer' (or 'Referer') header.host
- content of the HTTP 'Host' header.
method
- the request method in lower case (e.g.'get'
,'post'
).params
- an object where each key is a path parameter name with matching value as described in Path parameters.path
- the request URI's path component.payload
- an object containing the parsed request payload (when the routepayload
option is set to'parse'
).plugins
- plugin-specific state. Provides a place to store and pass request-level plugin data. Theplugins
is an object where each key is a plugin name and the value is the state.pre
- an object where each key is the name assigned by a route prerequisites function.query
- an object containing the query parameters.raw
- an object containing the Node HTTP server objects. Direct interaction with these raw objects is not recommended.req
- therequest
object.res
- the response object.
rawPayload
- the raw request payloadBuffer
(except when the routepayload
option is set to'stream'
).route
- the route configuration object after defaults are applied.server
- the server object.session
- Special key reserved for plugins implementing session support. Plugins utilizing this key must check fornull
value to ensure there is no conflict with another similar plugin.state
- an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition.url
- the parsed request URI.
Available only in 'onRequest'
extension methods.
Changes the request URI before the router begins processing the request where:
url
- the new request path value.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.ext('onRequest', function (request, next) {
// Change all requests to '/test'
request.setUrl('/test');
next();
});
Available only in 'onRequest'
extension methods.
Changes the request method before the router begins processing the request where:
method
- is the request HTTP method (e.g.'GET'
).
var Hapi = require('hapi');
var server = new Hapi.Server();
server.ext('onRequest', function (request, next) {
// Change all requests to 'GET'
request.setMethod('GET');
next();
});
Always available.
Logs request-specific events. When called, the server emits a 'request'
event which can be used by other listeners or plugins. The
arguments are:
tags
- a string or an array of strings (e.g.['error', 'database', 'read']
) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the'hapi'
tag along with event-specific information.data
- an optional message string or object with the application data being logged.timestamp
- an optional timestamp expressed in milliseconds. Defaults toDate.now()
(now).
var Hapi = require('hapi');
var server = new Hapi.Server();
server.on('request', function (event, tags) {
if (tags.error) {
console.log(event);
}
});
var handler = function () {
this.log(['test', 'error'], 'Test event');
};
Always available.
Returns an array containing the events matching any of the tags specified (logical OR) where:
tags
- is a single tag string or array of tag strings. If notags
specified, returns all events.
request.getLog();
request.getLog('error');
request.getLog(['hapi', 'error']);
Available until immediately after the 'response'
event is emitted.
Adds a request tail which has to complete before the request lifecycle is complete where:
name
- an optional tail name used for logging purposes.
Returns a tail function which must be called when the tail activity is completed.
Tails are actions performed throughout the request lifecycle, but which may end after a response is sent back to the client. For example, a request may trigger a database update which should not delay sending back a response. However, it is still desirable to associate the activity with the request when logging it (or an error associated with it).
When all tails completed, the server emits a 'tail'
event.
var Hapi = require('hapi');
var server = new Hapi.Server();
var get = function (request) {
var dbTail = request.tail('write to database');
db.save('key', 'value', function () {
dbTail();
});
request.reply('Success!');
};
server.route({ method: 'GET', path: '/', handler: get });
server.on('tail', function (request) {
console.log('Request completed including db activity');
});
Available until immediately after the 'onPreResponse'
extension point methods are called.
Sets a cookie which is sent with the response, where:
name
- the cookie name.value
- the cookie value. If noencoding
is defined, must be a string.options
- optional configuration. If the state was previously registered with the server usingserver.state()
, the specified keys inoptions
override those same keys in the server definition (but not others).
request.setState('preferences', { color: 'blue' }, { encoding: 'base64json' });
Available until immediately after the 'onPreResponse'
extension point methods are called.
Clears a cookie which sets an expired cookie and sent with the response, where:
name
- the cookie name.
request.clearState('preferences');
Available only within the handler method and only before one of request.reply()
, request.reply.redirection()
, request.reply.view()
, or
request.reply.close()
is called.
Concludes the handler activity by returning control over to the router where:
result
- an optional response payload.
Returns a response
object based on the value of result
:
null
,undefined
, or empty string''
-Empty
response.- string -
Text
response. Buffer
object -Buffer
response.Error
object (generated viaerror
ornew Error()
) -Boom
object.Stream
object -Stream
response.- any other object -
Obj
response.
var handler = function () {
this.reply('success');
};
The returned response
object provides a set of methods to customize the response (e.g. HTTP status code, custom headers, etc.). The methods
are response-type-specific and listed in response
.
var handler = function () {
this.reply('success')
.type('text/plain)
.header('X-Custom', 'some-value');
};
The response flow control rules apply.
Available only within the handler method and only before one of request.reply()
, request.reply.redirection()
, request.reply.view()
, or
request.reply.close()
is called.
Concludes the handler activity by returning control over to the router with a redirection response where:
uri
- an absolute or relative URI used to redirect the client to another resource. If a relative URI is provided, the value of the serverlocation
configuration option is used as prefix.
Returns a Redirection
response.
var handler = function () {
this.reply.redirection('http://example.com/elsewhere')
.message('You are being redirected...')
.permanent();
};
The response flow control rules apply.
Available only within the handler method and only before one of request.reply()
, request.reply.redirection()
, request.reply.view()
, or
request.reply.close()
is called.
Concludes the handler activity by returning control over to the router with a templatized view response where:
template
- the template filename and path, relative to the templates path configured via the serverviews.path
.context
- optional object used by the template to render context-specific result. Defaults to no context{}
.options
- optional object used to override the server'sviews
configuration for this response.
Returns a View
response.
var Hapi = require('hapi');
var server = new Hapi.Server({
views: {
engines: { html: 'handlebars' },
path: __dirname + '/templates'
}
});
var handler = function () {
var context = {
title: 'Views Example',
message: 'Hello, World'
};
this.reply.view('hello', context);
};
http.route({ method: 'GET', path: '/', handler: handler });
templates/hello.html
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<div>
<h1>{{message}}</h1>
</div>
</body>
</html>
The response flow control rules apply.
Available only within the handler method and only before one of request.reply()
, request.reply.redirection()
, request.reply.view()
, or
request.reply.close()
is called.
Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back
directly via request.raw.res
and that no further response action is needed (the router will ensure the request.raw.res
was ended).
No return value.
The response flow control rules do not apply.
Always available.
Returns a View
response object using the request environment where:
template
- the template filename and path, relative to the templates path configured via the serverviews.path
.context
- optional object used by the template to render context-specific result. Defaults to no context{}
.options
- optional object used to override the server'sviews
configuration for this response.
Useful when a view response is required outside of the handler (e.g. used in an extension point method to return an override response).
var Hapi = require('hapi');
var server = new Hapi.Server({ views: { engines: { html: 'handlebars' } } });
server.ext('onPreResponse', function (request, next) {
var response = request.response();
if (!response.isBoom) {
return next();
}
// Replace error with friendly HTML
var error = response;
var context = {
message: (error.response.code === 404 ? 'page not found' : 'something went wrong')
};
next(request.generateView('error', context));
});
Available after the handler method concludes and immediately after the 'onPreResponse'
extension point methods.
Returns the response object. The object can be modified but cannot be assigned another object. To replace the response with another
from within an extension point, use next(response)
to return a different response.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.ext('onPostHandler', function (request, next) {
var response = request.response();
if (response.variety === 'obj') {
delete response.raw._id; // Remove internal key
response.update();
}
next();
});
When calling request.reply()
, the router waits until process.nextTick()
to continue processing the request and transmit the response.
This enables making changes to the returned response object before the response is sent. This means the router will resume as soon as the handler
method exists. To suspend this behavior, the returned response
object includes:
response.hold()
- puts the response on hold untilresponse.send()
is called. Available only afterrequest.reply()
is called and untilresponse.hold()
is invoked once.response.send()
- resume the response which will be transmitted in the next tick. Available only afterresponse.hold()
is called and untilresponse.send()
is invoked once.
var handler = function () {
var response = this.reply('success').hold();
onTimeout(function () {
response.send();
}, 1000);
};
Every response type must include the following properties:
variety
- the response type name in lower case (e.g.'text'
).varieties
- an object where each key has atrue
value and represents a response type name (in lower case) whose functionality is made available via the response object (e.g. theFile
response typevarieties
object is{ generic: true, stream: true, file: true }
).
The Generic
response type is used as the parent prototype for all other response types. It cannot be instantiated directly and is only made available
for deriving other response types. It provides the following methods:
code(statusCode)
- sets the HTTP status code where:statusCode
- the HTTP status code.
header(name, value)
- sets an HTTP header where:name
- the header name.value
- the header value.
type(mimeType)
- sets the HTTP 'Content-Type' header where:value
- is the mime type. Should only be used to override the built-in default for each response type.
created(location)
- sets the HTTP status code to Created (201) and the HTTP 'Location' header where:location
- an absolute or relative URI used as the 'Location' header value. If a relative URI is provided, the value of the serverlocation
configuration option is used as prefix. Not available in theRedirection
response object.encoding(encoding)
- sets the 'Content-Type' HTTP header encoding property where:encoding
- the encoding property value.ttl(msec)
- overrides the default route cache expiration rule for this response instance where:msec
- the time-to-live value in milliseconds.
getTtl()
- returns the time-to-live value if an override has been set, and the request method is 'GET'.state(name, value, [options])
- sets an HTTP cookie as described inrequest.setState()
.unstate(name)
- clears the HTTP cookie by setting an expired value as described inrequest.clearState()
.
An empty response body (content-length of zero bytes). Supports all the methods provided by Generic
.
Generated with:
request.reply()
- without any arguments.new Hapi.response.Empty()
var handler1 = function () {
this.reply();
};
var handler2 = function () {
var response = new Hapi.response.Empty();
this.reply(response);
};
Plain text. The 'Content-Type' header defaults to 'text/html'
. Supports all the methods provided by Generic
as well as:
message(text, [type, [encoding]])
- sets or replace the response text where:text
- the text content.type
- the 'Content-Type' HTTP header value. Defaults to'text/html'
.encoding
- the 'Content-Type' HTTP header encoding property. Defaults to'utf-8'
.
Generated with:
request.reply(result)
- where:result
- must be a non-empty string.
new Hapi.response.Text(text, [type, [encoding]])
- same asmessage()
above.
var handler1 = function () {
this.reply('hello world');
};
var handler2 = function () {
var response = new Hapi.response.Text('hello world');
this.reply(response);
};
var handler3 = function () {
var response = new Hapi.response.Text();
response.message('hello world');
this.reply(response);
};
Buffer response. Supports all the methods provided by Generic
.
Generated with:
request.reply(result)
- where:result
- must be aBuffer
.
new Hapi.response.Buffer(buffer)
- where:buffer
- theBuffer
response.
var handler1 = function () {
var buffer = new Buffer([10, 11, 12, 13]);
this.reply(buffer);
};
var handler2 = function () {
var buffer = new Buffer([10, 11, 12, 13]);
var response = new Hapi.response.Buffer(buffer);
this.reply(response);
};
Replies with a stream object, directly piped into the HTTP response. Supports all the methods provided by Generic
as well as:
bytes(length)
- sets the HTTP 'Content-Length' header (to avoid chunked transfer encoding) where:length
- the header value. Must match the actual payload size.
Generated with:
request.reply(result)
- where:result
- must be aStream.Readable
or a node 0.8.xStream
.
new Hapi.response.Stream(stream)
- where:stream
- theStream.Readable
or a node 0.8.xStream
.
var Stream = require('stream');
var Hapi = require('hapi');
var ExampleStream = function () {
Stream.Readable.call(this);
};
Hapi.utils.inherits(ExampleStream, Stream.Readable);
ExampleStream.prototype._read = function (size) {
this.push('hello world');
this.push(null);
};
var handler1 = function () {
var stream = new ExampleStream();
this.reply(stream);
};
var handler2 = function () {
var response = new Hapi.response.Stream(new ExampleStream());
this.reply(response);
};
var handler3 = function () {
// Echo back request stream
this.reply(this.raw.req).bytes(this.raw.req.headers['content-length']);
};
JavaScript object, sent stringified. The 'Content-Type' header defaults to 'application/json'. Supports all the methods provided by
Generic
as well as:
raw
- the unmodified, unstringified object. Any direct manipulation must be followed withupdate()
.update(type, encoding)
- updates the string representation of the object response after changes toraw
where:type
- the 'Content-Type' HTTP header value. Defaults to'text/html'
.encoding
- the 'Content-Type' HTTP header encoding property. Defaults to'utf-8'
.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.ext('onPostHandler', function (request, next) {
var response = request.response();
if (response.variety === 'obj') {
delete response.raw._id; // Remove internal key
response.update();
}
next();
});
Generated with:
request.reply(result)
- where:result
- must be an object.
new Hapi.response.Obj(object, [type, [encoding]])
- where:object
- the response object.type
- the 'Content-Type' HTTP header value. Defaults to'text/html'
.encoding
- the 'Content-Type' HTTP header encoding property. Defaults to'utf-8'
.
var handler1 = function () {
this.reply({ message: 'hello world' });
};
var handler2 = function () {
var response = new Hapi.response.Obj({ message: 'hello world' });
this.reply(response);
};
Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. Supports all the methods
provided by Stream
.
Generated with:
new Hapi.response.File(filePath, [options])
- where:filePath
- a relative or absolute file path string (relative paths are resolved based on the serverfiles
configuration).options
- optional configuration:mode
- value of the HTTP 'Content-Disposition' header. Allowed values:'attachment'
'inline'
- the built-in route
file
handler.
var Hapi = require('hapi');
var server = new Hapi.Server({ files: { relativeTo: 'routes' } });
var handler1 = function () {
var response = new Hapi.response.File('./hello.txt');
this.reply(response);
};
server.route({ method: 'GET', path: '/1', handler: handler1 });
server.route({ method: 'GET', path: '/2', handler: { file: './hello.txt' } });
Transmits a file or list of files from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. This is an internal response time that can only be accessed via the built-in route handler.
Generated with:
- the built-in route
directory
handler.
var Hapi = require('hapi');
var server = new Hapi.Server({ files: { relativeTo: 'cwd' } });
var handler1 = {
directory: {
path: ['./public1/', './public2/'],
listing: true
}
};
var handler2 = {
directory: {
path: function (request) {
return (isMobileDevice(request) ? './mobile' : './public');
}
}
};
http.route({ method: 'GET', path: '/1/{path*}', handler: handler1 });
http.route({ method: 'GET', path: '/2/{path*}', handler: handler2 });
An HTTP redirection response (3xx). Supports all the methods provided by Text
(except for created()
) as well as the additional methods:
uri(dest)
- set the destination URI where:uri
- overrides the destination. An absolute or relative URI used as the 'Location' header value. If a relative URI is provided, the value of the serverlocation
configuration option is used as prefix.
temporary(isTemporary)
- sets the status code to302
or307
(based on therewritable()
setting) where:isTemporary
- iffalse
, sets status to permanent. Defaults totrue
.
permanent(isPermanent)
- sets the status code to301
or308
(based on therewritable()
setting) where:isPermanent
- iftrue
, sets status to temporary. Defaults tofalse
.
rewritable(isRewritable)
- sets the status code to301
/302
for rewritable (allows changing the request method from 'POST' to 'GET') or307
/308
for non-rewritable (does not allow changing the request method from 'POST' to 'GET'). Exact code based on thetemporary()
orpermanent()
setting. Arguments:isRewritable
- iffalse
, sets to non-rewritable. Defaults totrue
.
Permanent | Temporary | |
---|---|---|
Rewritable | 301 | 302(1) |
Non-rewritable | 308(2) | 307 |
Notes:
- Default value.
- Proposed code, not supported by all clients.
Generated with:
request.reply.redirect(uri)
- as described inrequest.reply.redirect()
.new Hapi.response.Redirection(uri, [message, [type, [encoding]]])
- where:uri
- an absolute or relative URI used as the 'Location' header value. If a relative URI is provided, the value of the serverlocation
configuration option is used as prefix.message
- the payload content. Defaults to'You are being redirected...'
.type
- the 'Content-Type' HTTP header value. Defaults to'text/html'
.encoding
- the 'Content-Type' HTTP header encoding property. Defaults to'utf-8'
.
var handler1 = function () {
this.reply.redirect('http://example.com/elsewhere')
.temporary().rewritable(false); // 307
};
var handler2 = function () {
var response = new Hapi.response.Redirection('http://example.com/elsewhere');
response.permanent().rewritable(); // 301
this.reply(response);
};
Template-based response. Supports all the methods provided by Generic
.
Generated with:
request.reply.view(template, [context, [options]])
- as described inrequest.reply.view()
.request.generateView(template, context, [options])
- as described inrequest.generateView()
.- the built-in route
view
handler.
var Hapi = require('hapi');
var server = new Hapi.Server({
views: {
engines: { html: 'handlebars' },
path: __dirname + '/templates'
}
});
var handler1 = function () {
var context = {
params: {
user: this.params.user
}
};
this.reply.view('hello', context);
};
var handler2 = function () {
var context = {
params: {
user: this.params.user
}
};
this.reply(this.generateView('hello', context));
};
server.route({ method: 'GET', path: '/1/{user}', handler: handler1 });
server.route({ method: 'GET', path: '/2/{user}', handler: handler2 });
server.route({ method: 'GET', path: '/3/{user}', handler: { view: 'hello' } });
templates/hello.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div>
<h1>About {{ params.user }}</h1>
</div>
</body>
</html>
The Cacheable
response type is used as the parent prototype for all cacheable response types. It cannot be instantiated directly and
is only made available for deriving other response types.
Provides a set of utilities for returning HTTP errors. An alias of the boom module (can be also accessed
Hapi.boom
). Each utility returns a Boom
error response object (instance of Error
) which includes the following properties:
isBoom
- iftrue
, indicates this is aBoom
object instance.message
- the error message.response
- the formatted response. Can be directly manipulated after object construction to return a custom error response. Allowed root keys:code
- the HTTP status code (typically 4xx or 5xx).headers
- an object containing any HTTP headers where each key is a header name and value is the header content.type
- a mime-type used as the content of the HTTP 'Content-Type' header (overridesheaders['Content-Type']
if present in both).payload
- the formatted object used as the response payload (stringified). Can be directly manipulated but any changes will be lost ifreformat()
is called. Any content allowed and by default includes the following content:code
- the HTTP status code, derived fromerror.response.code
.error
- the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived fromcode
.message
- the error message derived fromerror.message
.
- inherited
Error
properties.
It also supports the following method:
reformat()
- rebuildserror.response
using the other object properties.
var Hapi = require('hapi');
var handler = function () {
var error = Hapi.error.badRequest('Cannot feed after midnight');
error.response.code = 499; // Assign a custom error code
error.reformat();
this.reply(error);
});
Error responses return a JSON object with the code
, error
, and message
keys. When a different error representation is desired, such
as an HTML page or using another format, the 'onPreResponse'
extension point may be used to identify errors and replace them with a different
response object.
var Hapi = require('hapi');
var server = new Hapi.Server({ views: { engines: { html: 'handlebars' } } });
server.ext('onPreResponse', function (request, next) {
var response = request.response();
if (!response.isBoom) {
return next();
}
// Replace error with friendly HTML
var error = response;
var context = {
message: (error.response.code === 404 ? 'page not found' : 'something went wrong')
};
next(request.generateView('error', context));
});
Returns an HTTP Bad Request (400) error response object with the provided message
.
var Hapi = require('hapi');
Hapi.error.badRequest('Invalid parameter value');
Returns an HTTP Unauthorized (401) error response object where:
message
- the error message.scheme
- optional HTTP authentication scheme name (e.g.'Basic'
,'Hawk'
). If provided, includes the HTTP 'WWW-Authenticate' response header with the scheme and any providedattributes
.attributes
- an object where each key is an HTTP header attribute and value is the attribute content.
var Hapi = require('hapi');
Hapi.error.unauthorized('Stale timestamp', 'Hawk', { ts: fresh, tsm: tsm });
Returns an HTTP Unauthorized (401) error response object where:
message
- the error message.wwwAuthenticate
- an array of HTTP 'WWW-Authenticate' header responses for multiple challenges.
var Hapi = require('hapi');
Hapi.error.unauthorized('Missing authentication', ['Hawk', 'Basic']);
Returns an HTTP Request Timeout (408) error response object with the provided message
.
var Hapi = require('hapi');
Hapi.error.clientTimeout('This is taking too long');
Returns an HTTP Service Unavailable (503) error response object with the provided message
.
var Hapi = require('hapi');
Hapi.error.serverTimeout('Too busy, come back later');
Returns an HTTP Forbidden (403) error response object with the provided message
.
var Hapi = require('hapi');
Hapi.error.forbidden('Missing permissions');
Returns an HTTP Not Found (404) error response object with the provided message
.
var Hapi = require('hapi');
Hapi.error.notFound('Wrong number');
Returns an HTTP Internal Server Error (500) error response object where:
message
- the error message.data
- optional data used for error logging. Typically set to theError
object causing the failure.
The returned error object includes the following additional properties:
data
- thedata
object provided.trace
- the call stack leading to this error. Ifdata
is anError
object,trace
is set todata.trace
.outterTrace
- Ifdata
is anError
object, set to the call stack leading to this error, otherwisenull
.
Note that the error.response.payload.message
is overridden with 'An internal server error occurred'
to hide any internal details from
the client. error.message
remains unchanged.
var Hapi = require('hapi');
var handler = function () {
var result;
try {
result = JSON.parse(request.query.value);
}
catch (err) {
result = Hapi.error.internal('Failed parsing JSON input', err);
}
this.reply(result);
};
Returns a custom HTTP error response object where:
code
- the HTTP status code (typically 4xx or 5xx).payload
- the error payload string orBuffer
.contentType
- a mime-type used as the content of the HTTP 'Content-Type' header (overridesheaders['Content-Type']
if present in both).headers
- an object containing any HTTP headers where each key is a header name and value is the header content.
Used to pass-through errors received from upstream services (proxied) when a response should be treated internally as an error but contain custom properties.
var Hapi = require('hapi');
Hapi.error.passThrough(404, '<h1>Not Found</h1>', 'text/html', { 'Cache-Control': 'no-cache' });
Pack
is a collection of servers grouped together to form a single logical unit. The pack's primary purpose is to provide a unified object
interface when working with plugins. Grouping multiple servers into a single pack enables treating them as a single
entity which can start and stop in sync, as well as enable sharing routes and other facilities.
The servers in a pack share the same cache. Every server belongs to a pack, even if created directed via
new Server()
, in which case the server.pack
object is automatically assigned a single-server pack.
Creates a new Pack
object instance where:
options
- optional configuration:app
- an object used to initialize the application-specific data stored inpack.app
.cache
- cache configuration as described in the servercache
option.requirePath
- sets the path from which plugins are loaded. Defaults to current working directory.
var Hapi = require('hapi');
var pack = new Hapi.Pack();
Each Pack
object instance has the following properties:
app
- application-specific state. Provides a safe place to store application data without potential conflicts with hapi. Initialized via the packapp
configuration option. Defaults to{}
.events
- anEvents.EventEmitter
providing a consolidate emitter of all the events emitted from all member pack servers.list
- an object listing all the registered plugins where each key is a plugin name and the value is an object with:name
- plugin name.version
- plugin version.path
- the plugin root path (where 'package.json' is located).register()
- theexports.register()
function.
Creates a Server
instance and adds it to the pack, where host
, port
, options
are the same as described in
new Server()
with the exception that the cache
option is not allowed and must be
configured via the pack cache
option.
var Hapi = require('hapi');
var pack = new Hapi.Pack();
pack.server(8000, { labels: ['web'] });
pack.server(8001, { labels: ['admin'] });
Starts all the servers in the pack and used as described in server.start([callback])
.
var Hapi = require('hapi');
var pack = new Hapi.Pack();
pack.server(8000, { labels: ['web'] });
pack.server(8001, { labels: ['admin'] });
pack.start(function () {
console.log('All servers started');
});
Stops all the servers in the pack and used as described in server.stop([options], [callback])
.
pack.stop({ timeout: 60 * 1000 }, function () {
console.log('All servers stopped');
});
Overrides the default plugin permissions when requiring or registering a plugin. Where:
permissions
- an object where each key is a permission name and the value is a boolean set totrue
(allow) orfalse
(deny) access.
Returns a plugin registration interface with the pack.require()
and pack.register()
methods.
The default permissions are:
auth
- allows registering an authentication strategy viaplugin.auth()
. Defaults totrue
.cache
- allows provisioning a plugin cache segment viaplugin.cache()
. Defaults totrue
.events
- allows access to events viaplugin.events
. Defaults totrue
.ext
- allows registering extension methods viaplugin.ext()
. Defaults tofalse
.helper
- allows addming server helper methods viaplugin.helper()
. Defaults totrue
.route
- allows adding routes viaplugin.route()
. Defaults totrue
.state
- allows configuring state definitions viaplugin.state()
. Defaults totrue
.views
- allows configuring a plugin-specific views manager viaplugin.views()
. Defaults totrue
.
var Hapi = require('hapi');
var pack = new Hapi.Pack();
pack.server(8000, { labels: ['web'] });
pack.server(8001, { labels: ['admin'] });
pack.allow({ ext: true }).require('yar', function (err) {
if (err) {
console.log('Failed loading plugin: yar');
}
});
Registers a plugin where:
name
- the node module name as expected by node'srequire()
. Ifname
is a relative path, prefixed with the value of the packrequirePath
configuration option.options
- optional configuration object which is passed to the plugin via theoptions
argument inexports.register()
. Ifoptions
is an array, the first array item is used aspermissions
, and the second item is used asoptions
.callback
- the callback function with signaturefunction(err)
where: -err
- an error returned fromexports.register()
. Note that incorrect usage, bad configuration, missing permissions, or namespace conflicts (e.g. among routes, helpers, state) will throw an error and will not return a callback.
pack.require('furball', { version: '/v' }, function (err) {
if (err) {
console.log('Failed loading plugin: furball');
}
});
Registers a list of plugins where:
names
- an array of plugins names as described inpack.require()
, or an object in which each key is a plugin name, and each value is theoptions
object used to register that plugin. If theoptions
value is an array, the first array item is used aspermissions
, and the second item is used asoptions
.callback
- the callback function with signaturefunction(err)
where: -err
- an error returned fromexports.register()
. Note that incorrect usage, bad configuration, missing permissions, or namespace conflicts (e.g. among routes, helpers, state) will throw an error and will not return a callback.
Batch registration is required when plugins declare a dependency, so that all the required dependencies are loaded in a single transaction (internal order does not matter).
pack.require(['furball', 'lout'], function (err) {
if (err) {
console.log('Failed loading plugin: furball');
}
});
pack.require({ furball: null, lout: { endpoint: '/docs' } }, function (err) {
if (err) {
console.log('Failed loading plugins');
}
});
Registers a plugin object (without using require()
) where:
plugin
- the plugin object which requires:name
- plugin name.version
- plugin version.path
- optional plugin path for resolving relative paths used by the plugin. Defaults to current working directory.register()
- theexports.register()
function.
options
- optional configuration object which is passed to the plugin via theoptions
argument inexports.register()
. Ifoptions
is an array, the first array item is used aspermissions
, and the second item is used asoptions
.callback
- the callback function with signaturefunction(err)
where:err
- an error returned fromexports.register()
. Note that incorrect usage, bad configuration, missing permissions, or namespace conflicts (e.g. among routes, helpers, state) will throw an error and will not return a callback.
var plugin = {
name: 'test',
version: '2.0.0',
register: function (pack, options, next) {
pack.route({ method: 'GET', path: '/special', handler: function () { this.reply(options.message); } } );
next();
}
};
server.pack.register(plugin, { message: 'hello' }, function (err) {
if (err) {
console.log('Failed loading plugin');
}
});
The Composer
provides a simple way to construct a Pack
from a single configuration object, including configuring servers
and registering plugins.
Creates a Composer
object instance where:
manifest
- an object or array or objects where:pack
- the packoptions
as described innew Pack()
.server
- an array of server configuration object where:host
,port
,options
- the same as described innew Server()
with the exception that thecache
option is not allowed and must be configured via the packcache
option.
plugin
- an object where each key is a plugin name, and each value is theoptions
object used to register that plugin. If theoptions
value is an array, the first array item is used aspermissions
, and the second item is used asoptions
.
var Hapi = require('hapi');
var manifest = {
pack: {
cache: 'memory'
},
servers: [
{
port: 8000,
options: {
labels: ['web']
}
},
{
host: 'localhost',
port: 8001,
options: {
labels: ['admin']
}
}
],
plugins: {
'yar': [
{
ext: true
},
{
cookieOptions: {
password: 'secret'
}
}
]
}
};
var composer = new Hapi.Composer(manifest);
Creates the packs described in the manifest construction where:
callback
- the callback method, called when all packs and servers have been created and plugins registered has the signaturefunction(err)
where:err
- an error returned fromexports.register()
. Note that incorrect usage, bad configuration, missing permissions, or namespace conflicts (e.g. among routes, helpers, state) will throw an error and will not return a callback.
composer.compose(function (err) {
if (err) {
console.log('Failed composing');
}
});
Starts all the servers in all the pack composed where:
callback
- the callback method called when all the servers have been started.
composer.start(function () {
console.log('All servers started');
});
Stops all the servers in all the packs and used as described in server.stop([options], [callback])
.
pack.stop({ timeout: 60 * 1000 }, function () {
console.log('All servers stopped');
});
Plugins provide an extensibility platform for both general purpose utilities such as batch requests and for application business logic. Instead of thinking about a web server as a single entity with a unified routing table, plugins enable developers to break their application into logical units, assembled together in different combinations to fit the development, testing, and deployment needs.
Constructing a plugin requires the following:
- name - the plugin name is used as a unique key. Public plugins should be published in the npm registry and derive their name
from the registry name. This ensures uniqueness. Private plugin names should be picked carefully to avoid conflicts with both private and public
names. Typically, private plugin names use a prefix such as the company name or an unusual combination of characters (e.g.
'--'
). When using thepack.require()
interface, the name is obtained from the 'package.json' module file. When using thepack.register()
interface, the name is provided as a required key inplugin
. - version - the plugin version is only used informatively within the framework but plays an important role in the plugin echo-system. The plugin
echo-system relies on the npm peer dependency functionality to ensure that plugins can
specify their dependency on a specific version of hapi, as well as on each other. Dependencies are expressed solely within the 'package.json'
file, and are enforced by npm. When using the
pack.require()
interface, the version is obtained from the 'package.json' module file. When using thepack.register()
interface, the version is provided as a required key inplugin
. exports.register()
- the registration function described inexports.register()
is the plugin's core. The function is called when the plugin is registered and it performs all the activities required by the plugin to operate. It is the single entry point into the plugin functionality. When using thepack.require()
interface, the function is obtained byrequire()
'ing the plugin module and invoking the exportedregister()
method. When using thepack.register()
interface, the function is provided as a required key inplugin
.
package.json
{
"name": "furball",
"description": "Plugin utilities and endpoints",
"version": "0.3.0",
"main": "index",
"dependencies": {
"hoek": "0.8.x"
},
"peerDependencies": {
"hapi": "1.x.x"
}
}
index.js
var Hoek = require('hoek');
var internals = {
defaults: {
version: '/version',
plugins: '/plugins'
}
};
internals.version = Hoek.loadPackage().version;
exports.register = function (plugin, options, next) {
var settings = Hoek.applyToDefaults(internals.defaults, options);
if (settings.version) {
plugin.route({
method: 'GET',
path: settings.version,
handler: function () {
this.reply(internals.version);
}
});
}
if (settings.plugins) {
plugin.route({
method: 'GET',
path: settings.plugins,
handler: function () {
this.reply(listPlugins(this.server));
}
});
}
var listPlugins = function (server) {
var plugins = [];
Object.keys(server.pack.list).forEach(function (name) {
var item = server.pack.list[name];
plugins.push({
name: item.name,
version: item.version
});
});
return plugins;
};
plugin.api('plugins', listPlugins);
next();
};
Registers the plugin where:
plugin
- the registration interface representing the pack the plugin is being registered into. Provides the properties and methods listed below, based on the permissions granted.options
- theoptions
object provided by the pack registration methods.next
- the callback function the plugin must call to return control over to the application and complete the registration process. The function signature isfunction(err)
where:err
- internal plugin error condition, which is returned back via the registration methods' callback. A plugin registration error is considered an unrecoverable event which should terminate the application.
exports.register = function (plugin, options, next) {
plugin.route({ method: 'GET', path: '/', handler: function () { this.reply('hello world') } });
next();
};
The plugin interface root methods and properties are those available only on the plugin
object received via the
exports.register()
interface. They are not available on the object received by calling
plugin.select()
.
The plugin version information.
exports.register = function (plugin, options, next) {
console.log(plugin.version);
next();
};
The plugin root path (where 'package.json' resides).
var Fs = require('fs');
exports.register = function (plugin, options, next) {
var file = Fs.readFileSync(plugin.path + '/resources/image.png');
next();
};
A reference to the hapi module used to create the pack and server instances. Removes the need to add a dependency on hapi within the plugin.
exports.register = function (plugin, options, next) {
var Hapi = plugin.hapi;
var handler = function () {
this.reply(Hapi.error.internal('Not implemented yet'));
};
plugin.route({ method: 'GET', path: '/', handler: handler });
next();
};
Provides access to the common pack application-specific state.
exports.register = function (plugin, options, next) {
plugin.app.hapi = 'joi';
next();
};
Requires the events
plugin permission.
The `pack.events' emitter.
exports.register = function (plugin, options, next) {
plugin.events.on('internalError', function (request, err) {
console.log(err);
});
next();
};
Emits a 'log'
event on the pack.events' emitter using the same interface as [
server.log()`](#serverlogtags-data-timestamp).
exports.register = function (plugin, options, next) {
plugin.log(['plugin', 'info'], 'Plugin registered');
next();
};
Declares a required dependency other plugins where:
deps
- a single string or array of strings of plugin names which must be registered in order for this plugin to operate. Plugins listed must be registered in the same pack transaction to allow validation of the dependency requirements. Does not provide version dependency which should be implemented using npm peer dependencies.
exports.register = function (plugin, options, next) {
plugin.dependecy('yar');
next();
};
Requires the views
plugin permission.
Generates a plugin-specific views manager for rendering templates where:
options
- the views configuration as described in the server'sviews
option.
Note that relative paths are relative to the plugin root, not the working directory or the application registering the plugin. This allows plugin the specify their own static resources without having to require external configuration.
exports.register = function (plugin, options, next) {
plugin.views({
engines: {
jade: 'jade'
},
path: './templates'
});
next();
};
Requires the helper
plugin permission.
Registers a server helper function with all the pack's servers as described in server.helper()
exports.register = function (plugin, options, next) {
plugin.helper('user', function (id, next) {
next({ id: id });
});
next();
};
Requires the cache
plugin permission.
Provisions a plugin cache segment within the pack's common caching facility where:
options
- cache configuration as described in catbox module documentation:expiresIn
- relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together withexpiresAt
.expiresAt
- time of day expressed in 24h notation using the 'MM:HH' format, at which point all cache records for the route expire. Cannot be used together withexpiresIn
.staleIn
- number of milliseconds to mark an item stored in cache as stale and reload it. Must be less thanexpiresIn
.staleTimeout
- number of milliseconds to wait before checking if an item is stale.segment
- optional segment name, used to isolate cached items within the cache partition. Defaults to '!name' where 'name' is the plugin name. When setting segment manually, it must begin with '!!'.
exports.register = function (plugin, options, next) {
var cache = plugin.cache({ expiresIn: 60 * 60 * 1000 });
next();
};
The plugin interface selectable methods and properties are those available both on the plugin
object received via the
exports.register()
interface and the objects received by calling
plugin.select()
. However, unlike the root methods, they operate only on the selected subset of
servers.
Selects a subset of pack servers using the servers' labels
configuration option where:
labels
- a single string or array of strings of labels used as a logical OR statement to select all the servers with matching labels in their configuration.
Returns a new plugin
interface with only access to the selectable methods and properties.
Selecting again on a selection operates as a logic AND statement between the individual selections.
exports.register = function (plugin, options, next) {
var selection = plugin.select('web');
selection.route({ method: 'GET', path: '/', handler: 'notfound' });
next();
};
The number of selected servers.
exports.register = function (plugin, options, next) {
var count = plugin.lenght;
var selectedCount = plugin.select('web').length;
next();
};
Adds an plugin API to the server.plugins[name]
('name' of plugin) object of each selected pack server where:
key
- the key assigned (server.plugins[name][key]
).value
- the value assigned.
exports.register = function (plugin, options, next) {
plugin.api('util', function () { console.log('something'); });
next();
};
Merges a deep copy of an object into to the existing content of the server.plugins[name]
('name' of plugin) object of each
selected pack server where:
obj
- the object merged into the API container.
exports.register = function (plugin, options, next) {
plugin.api({ util: function () { console.log('something'); } });
next();
};
Requires the route
plugin permission.
Adds a server route to the selected pack's servers as described in server.route(options)
.
exports.register = function (plugin, options, next) {
var selection = plugin.select('web');
selection.route({ method: 'GET', path: '/', handler: 'notfound' });
next();
};
Requires the route
plugin permission.
Adds multiple server routes to the selected pack's servers as described in server.route(routes)
.
exports.register = function (plugin, options, next) {
var selection = plugin.select('admin');
selection.routes([
{ method: 'GET', path: '/1', handler: 'notfound' },
{ method: 'GET', path: '/2', handler: 'notfound' }
]);
next();
};
Requires the state
plugin permission.
Adds a state definition to the selected pack's servers as described in server.state()
.
exports.register = function (plugin, options, next) {
plugin.state('example', { encoding: 'base64' });
next();
};
Requires the auth
plugin permission.
Adds an authentication strategy to the selected pack's servers as described in server.auth()
.
exports.register = function (plugin, options, next) {
plugin.auth('simple', {
scheme: 'basic',
validateFunc: function (username, password, callback) {
callback(new Error('User not found'));
}
});
next();
};
Requires the ext
plugin permission.
Adds an extension point method to the selected pack's servers as described in server.ext()
.
exports.register = function (plugin, options, next) {
plugin.ext('onRequest', function (request, extNext) {
console.log('Received request: ' + request.path);
extNext();
});
next();
};
An alias of the hoek module.
Returns the hapi module version number.
var Hapi = require('hapi');
console.log(Hapi.version());
See joi Types.
Prepares a cookie value manually outside of the normal outgoing cookies processing flow. Used when decisions have to be made about the use of cookie values when certain conditions are met (e.g. stringified object string too long). Arguments:
name
- the cookie name.value
- the cookie value. If noencoding
is defined, must be a string.options
- configuration override. If the state was previously registered with the server usingserver.state()
, the specified keys inoptions
override those same keys in the server definition (but not others).callback
- the callback function with signaturefunction(err, value)
where:err
- internal error condition.value
- the prepared cookie value.
Returns the cookie value via callback without making any changes to the response.
var Hapi = require('hapi');
var handler = function (request) {
var maxCookieSize = 512;
var cookieOptions = {
encoding: 'iron',
password: 'secret'
};
var content = request.pre.user;
Hapi.state.prepareValue('user', content, cookieOptions, function (err, value) {
if (err) {
return request.reply(err);
}
if (value.length < maxCookieSize) {
request.setState('user', value, { encoding: 'none' } ); // Already encoded
}
request.reply('success');
});
};