fridays / next-routes

Universal dynamic routes for Next.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using HTTP2 and Next-routes together

leo-petrucci opened this issue · comments

I'm having trouble finding any information about how to implement HTTP2 while also still using Next Routes. Next Routes is an absolute lifesaver and has cut down the amount of work I have to do a lot so I'd really prefer not having to ditch it.

This is my current server.js script:

const express = require( 'express' );
const next    = require( 'next' );

// Import middleware.
const routes = require( './routes' );

// Setup app.
const app     = next( { dev: 'production' !== process.env.NODE_ENV } );
const handle  = app.getRequestHandler();
const handler = routes.getRequestHandler( app );

app.prepare()
  .then( () => {

    // Create server.
    const server = express();

    // Use our handler for requests.
    server.use( handler );

    // Don't remove. Important for the server to work. Default route.
    server.get( '*', ( req, res ) => {
      return handle( req, res );
    } );

    // Get current port.
    const port = process.env.PORT || 8080;

    // Error check.
    server.listen( port, err => {
      if ( err ) {
        throw err;
      }

      // Where we starting, yo!
      console.log( `> Ready on port ${port}...` );
    } );
  } );

And my routes.js script:

const routes = require( 'next-routes' );

// Setup router.
module.exports = routes()
  .add( 'index', '/' )
  .add( 'posts' )
  .add( 'single', '/posts/:slug' );

I've been trying to implement this example into my current server, unfortunately when not using express I can't use server.use( handler ); which means I have absolutely no idea how to use the handler.

Am I trying to do something impossible or is this an easy solution?

Sorry to dissapoint you, but as far as I can tell, there are no express http2 solution supported, but they are working on it:
expressjs/express#3730

You can use something like node-spdy without using express, and there is instruction on how to use next-routes without express on readme.

Other solution that i used before, more hacky one is to create regular express server and proxy it through http2 on nginx

Its not hacky to use nginx as a proxy - its actually preferred. Node should not be directly exposed.