rsnext/examples/with-http2/server.js
Shahzeb K c97617fdf5 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](31877720f0/examples/with-http2/README.md (download-manually)) about how to generate the public/private keys used for this demo server.
2019-04-03 11:31:45 +02:00

36 lines
953 B
JavaScript

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}`)
})