Commit c97617fd authored by Shahzeb K's avatar Shahzeb K Committed by Tim Neutkens

Added an HTTP2 server example (#6856)

As requested in this issue: https://github.com/zeit/next.js/issues/6850

This is a basic HTTP2 server which can handle the `/` and `/about` routes. 

The Readme includes [specific instruction](https://github.com/shahzeb1/next.js/blob/31877720f076b49b39a167deb0ba8650d0052dda/examples/with-http2/README.md#download-manually) about how to generate the public/private keys used for this demo server.
parent b509841f
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-http2)
# HTTP2 server example
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
```bash
npx create-next-app --example with-http2 with-http2-app
# or
yarn create next-app --example with-http2 with-http2-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-http2
cd hello-world
```
Create the public and private keys:
```bash
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout localhost-privkey.pem -out localhost-cert.pem
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example shows the most basic example using an HTTP2 server. We have 2 pages: `pages/index.js` and `pages/about.js`. The former responds to `/` requests and the latter to `/about`. Using `next/link` you can add hyperlinks between them with universal routing capabilities.
{
"name": "with-http2",
"version": "1.0.0",
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"next": "8.0.4",
"react": "16.8.6",
"react-dom": "16.8.6"
},
"license": "ISC"
}
import Link from 'next/link'
export default () => (
<div>
<h3>This is the /about page. </h3>
<Link href='/'>
<a> &larr; Back home</a>
</Link>
</div>
)
import Link from 'next/link'
export default () => (
<div>
<h3>Hello World.</h3>
<Link href='/about'>
<a>About</a>
</Link>
</div>
)
const next = require('next')
const http2 = require('http2')
const fs = require('fs')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
// Init the Next app:
const app = next({ dev })
// Create the secure HTTPS server:
// Don't forget to create the keys for your development
const server = http2.createSecureServer({
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem')
})
app.prepare().then(() => {
server.on('error', err => console.error(err))
// Process the various routes based on `req`
// `/` -> Render index.js
// `/about` -> Render about.js
server.on('request', (req, res) => {
switch (req.url) {
case '/about':
return app.render(req, res, '/about', req.query)
default:
return app.render(req, res, '/', req.query)
}
})
server.listen(port)
console.log(`Listening on HTTPS port ${port}`)
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment