UNPKG

1.2 kBJavaScriptView Raw
1'use strict';
2const debug = require('debug')('alexia:debug');
3const info = require('debug')('alexia:info');
4
5/**
6 * Creates Hapi server with one route that handles all requests with app specified in param. Server must be started using `server.start()`
7 * @param {object} app - App created using alexia.createApp(...)
8 * @param {number} [options] - Server options
9 * @property {number} [options.path] - Path to run server route on. Defaults to `/`
10 * @property {number} [options.port] - Port to run server on. If not specified then `process.env.PORT` is used. Defaults to `8888`
11 * @returns {object} server
12 */
13module.exports = (app, options) => {
14 const Hapi = require('hapi');
15 const server = new Hapi.Server();
16
17 options = Object.assign({}, options);
18
19 server.connection({
20 port: options.port || process.env.PORT || 8888
21 });
22
23 server.route({
24 path: options.path || '/',
25 method: 'POST',
26 handler: (request, response) => {
27 app.handle(request.payload, (data) => {
28 response(data);
29 });
30 }
31 });
32
33 info(`Server created on URI: "${server.info.uri}"`);
34 debug(`Server created on URI: "${server.info.uri}"`);
35 return server;
36};