question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to deal with http2 ?

See original GitHub issue

Context

  • hapi-swagger version: 9.1.3
  • any other relevant information: node 10.9.0

What are you trying to achieve or the steps to reproduce?

I currently use hapi-swagger and hapi-swagered-ui with http. I want to migrate to http2.

No problem to reach my routes when I use http2 but impossible to run hapi-swagger.

import * as http2 from 'http2';
import * as fs from 'fs';
import * as path from 'path';

import Hapi from 'hapi';
import Inert from 'inert';
import Vision from 'vision';
import HapiSwagger from 'hapi-swagger';
import HapiSwaggerUI from 'hapi-swaggered-ui';

const port = 5000;
const protocol = 'http2';

const getPath = filepath => path.resolve(__dirname, filepath);
const getFile = async (filepath, encoding = 'utf8') => fs.readFileSync(getPath(filepath), encoding);

const getHttp2Listener = async () => http2.createSecureServer({
  key: await getFile('./certs/server.key'),
  cert: await getFile('./certs/server.crt'),
});

async function StartServer() {
  const server = new Hapi.Server(
    protocol === 'http2'
      ? { listener: await getHttp2Listener(), tls: true, port }
      : { host: 'localhost', port },
  );

  await server.register([
    Inert,
    Vision,
    {
      plugin: HapiSwagger,
      options: {
        documentationPage: false,
      },
    },
    {
      plugin: HapiSwaggerUI,
      options: {
        title: 'OpenJam API Documentation',
        path: '/docs',
        authorization: null,
        swaggerEndpoint: '/swagger.json',
      },
      swaggerOptions: {},
    },
  ]);

  server.route([
    {
      method: 'GET',
      path: '/ping',
      handler: () => ({ pong: true }),
    },
  ]);

  await server.start();
  console.log(`Protocol: ${protocol}`);
  console.log(`Server running on ${port}...`);
  console.log(`Check-out ${server.info.uri} `);
}

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

StartServer().catch(err => console.log(err));

What result did you get?

Failed to load resource: the server responded with a status of 500 ()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
morgangraphicscommented, Feb 3, 2019

The issue above could also be related to ...? { listener: await getHttp2Listener(), tls: true, port } http2.createSecureServer is TLS/https by default so it seems redundant (maybe a collision) to enable HAPI tls: true and have the listener on https as well. In any rate I believe it’s a configuration issue and not anything wrong with hapi-swagger.

0reactions
robmcguinnesscommented, Feb 2, 2019

@morgangraphics thanks for analysis. I’ve tagged the issue for documentation. Probably a good opportunity to add an example or some docs in the README.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to HTTP/2 - web.dev
The primary goals for HTTP/2 are to reduce latency by enabling full request and response multiplexing, minimize protocol overhead via efficient compression of ......
Read more >
HTTP/2 Frequently Asked Questions
Clients looking to implement HTTP/2 only will need to treat HTTP/1.1 responses without a 101 status code as errors. h2c -only servers can...
Read more >
How to Use HTTP/2 to Speed Up Your Websites and Apps
You need to get an SSL certificate and move to HTTPS because Firefox and Chrome only support HTTP/2 over TLS using the new...
Read more >
7 Tips for Faster HTTP/2 Performance
Decide if You Need HTTP/2 Today; Terminate HTTP/2 and TLS; Consider Starting with SPDY; Identify HTTP/1.x Optimizations in Your Code; Implement ...
Read more >
What is HTTP/2 – The Ultimate Guide
This in-depth guide explains HTTP/2 for end-users and developers. From basic information to more advanced topics.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found