├── README.md ├── package.json ├── pages ├── about.js └── index.js └── server.js /README.md: -------------------------------------------------------------------------------- 1 | # Custom Express HTTP/2 Server example 2 | 3 | ## How to use 4 | 5 | ### Download manually 6 | 7 | Download the example: 8 | 9 | ```bash 10 | git clone https://github.com/typedef42/nextjs-express-http2-example.git 11 | cd nextjs-express-http2-example 12 | ``` 13 | 14 | Create the public and private keys: 15 | 16 | ```bash 17 | openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \ 18 | -keyout privateKey.key -out certificate.cert 19 | ``` 20 | 21 | Install it and run: 22 | 23 | ```bash 24 | npm install 25 | npm run dev 26 | # or 27 | yarn 28 | yarn dev 29 | ``` 30 | 31 | ## The idea behind the example 32 | 33 | Most of the times the default Next server will be enough but sometimes you want to run your own server to customize routes or other kind of the app behavior. Next provides a [Custom server and routing](https://github.com/zeit/next.js#custom-server-and-routing) so you can customize as much as you want. 34 | 35 | Because the Next.js server is just a node.js module you can combine it with any other part of the node.js ecosystem. In this case we are using a HTTP/2 server with the spdy package. Beside its name, the package spdy provides support for both http/2 (h2) and spdy (2,3,3.1), and allow to be combined with express to build a custom router on top of Next and serve content through HTTP/2. 36 | 37 | The example shows a server that render and serves two single pages when their corresponding route are requested. You can obviously use more advanced express routing strategy depending on your needs. 38 | 39 | Since no major browser support HTTP/2 without encryption, don't forget to generate self-signed local certificate for development purpose. 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nextjs-express-http2-example", 3 | "author": "Yannis Torres", 4 | "license": "MIT", 5 | "version": "1.0.0", 6 | "scripts": { 7 | "dev": "node server.js", 8 | "build": "next build", 9 | "release": "cross-env NODE_ENV=production next build", 10 | "start": "cross-env NODE_ENV=production node server.js" 11 | }, 12 | "dependencies": { 13 | "compression": "^1.7.4", 14 | "express": "^4.17.1", 15 | "next": "latest", 16 | "react": "^16.7.0", 17 | "react-dom": "^16.7.0", 18 | "spdy": "^4.0.1" 19 | }, 20 | "devDependencies": { 21 | "cross-env": "^5.2.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pages/about.js: -------------------------------------------------------------------------------- 1 | import Link from 'next/link' 2 | export default () => ( 3 |