Hapi: Hapi and Support of HTTP/2

Created on 21 Apr 2015  路  9Comments  路  Source: hapijs/hapi

Hello,

Does Hapy have any support of HTTP/2 in natif or by plugins ?

Thank by advance

support

Most helpful comment

@casalot I had luck doing this:

'use strict'
const fs = require('fs')
const Hapi = require('hapi')
const Http2 = require('http2')
const server = new Hapi.Server()

let listener = Http2.createServer({
  key: fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./cert.pem')
})

if (!listener.address) {
  listener.address = function() {
    return this._server.address()
  }
}

server.connection({
  listener: listener,
  port: '8000',
  tls: true
})

server.route({
  method: 'GET',
  path: '/',
  handler: (request, reply) => reply('hello world')
})

server.start(err => {
  if (err) console.error(err)
  console.log(`Started ${server.connections.length} connections`)
})

All 9 comments

You can provide your own server implementation when creating a connection using the listener option https://github.com/hapijs/hapi/blob/master/API.md#serverconnectionoptions.

This comes down to: you can provide your own http2 node server implementation until node/iojs implements it right?

it is working like express ?

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello, http2!')
})

var options = {
  key: fs.readFileSync('./example/localhost.key'),
  cert: fs.readFileSync('./example/localhost.crt')
};

require('http2').createServer(options, app).listen(8080);

It not work like as if I looked the socket.io sample
http://matt-harrison.com/using-hapi-js-with-socket-io/

I will try it with node-http2 module..

I couldn't find any resources online with a direct explanation of how to implement HTTP2 with Hapi so I'll document my solution here:

var Fs = require('fs');
var Hapi = require('hapi');
var Http2 = require('http2');

var options = {
  key: Fs.readFileSync('./ssl/site.key');
  cert: Fs.readFileSync('./ssl/site.crt');
};

var server = new Hapi.Server();
server.connection({
   listener: Http2.createServer(options),
   host: 'localhost',
   port: 8080,
   tls: true
});

It would appear that Socket.io is NOT yet compatible with the HTTP2 library. I've submitted a bug report. The workaround is serving Socket.io from another connection with the usual HTTPS listener.

@timelessvirtues, I've tried your sample above, but I keep getting a self.listener.address is not a function. Any ideas?

@casalot I had luck doing this:

'use strict'
const fs = require('fs')
const Hapi = require('hapi')
const Http2 = require('http2')
const server = new Hapi.Server()

let listener = Http2.createServer({
  key: fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./cert.pem')
})

if (!listener.address) {
  listener.address = function() {
    return this._server.address()
  }
}

server.connection({
  listener: listener,
  port: '8000',
  tls: true
})

server.route({
  method: 'GET',
  path: '/',
  handler: (request, reply) => reply('hello world')
})

server.start(err => {
  if (err) console.error(err)
  console.log(`Started ${server.connections.length} connections`)
})

Hi, I am trying to add the https config tls to manifest js as below

connections: [{
host: {
$filter: 'env',
production: process.env.HOST,
$default: '0.0.0.0'
},
port: {
$filter: 'env',
production: process.env.PORT,
$default: 3000

},
tls:{
key : fs.readFileSync('testkey.pem'),
cert : fs.readFileSync('testcert.pem')
}}]

But https server is not getting started. Could anyone help me on fixing this.

Please avoid hijacking unrelated threads when you already have yours.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DrMabuse23 picture DrMabuse23  路  5Comments

foobar1123 picture foobar1123  路  3Comments

hueniverse picture hueniverse  路  4Comments

hovmand picture hovmand  路  3Comments

taoeffect picture taoeffect  路  3Comments