mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
5d779a0289
After discussion, I added falling back to fetch based pinging when the WebSocket fails to connect. I also added an example of how to proxy the onDemandEntries WebSocket when using a custom server. Fixes: #6296
44 lines
1 KiB
JavaScript
44 lines
1 KiB
JavaScript
const express = require('express')
|
|
const Next = require('next')
|
|
const https = require('https')
|
|
const fs = require('fs')
|
|
const app = express()
|
|
const port = 3000
|
|
const isDev = process.env.NODE_ENV !== 'production'
|
|
const next = Next({ dev: isDev })
|
|
|
|
// Set up next
|
|
next.prepare()
|
|
|
|
// Set up next handler
|
|
app.use(next.getRequestHandler())
|
|
|
|
// Set up https.Server options with SSL
|
|
const options = {
|
|
key: fs.readFileSync('./localhost.key'),
|
|
cert: fs.readFileSync('./localhost.cert')
|
|
}
|
|
|
|
// Create http server using express app as requestHandler
|
|
const server = https.createServer(options, app)
|
|
|
|
// Set up proxying for Next's onDemandEntries WebSocket to allow
|
|
// using our SSL
|
|
if (isDev) {
|
|
const CreateProxyServer = require('http-proxy').createProxyServer
|
|
const proxy = CreateProxyServer({
|
|
target: {
|
|
host: 'localhost',
|
|
port: 3001
|
|
}
|
|
})
|
|
|
|
server.on('upgrade', (req, socket, head) => {
|
|
proxy.ws(req, socket, head)
|
|
})
|
|
}
|
|
|
|
server.listen(port, () => {
|
|
console.log(`Server listening at http://localhost:${port}`)
|
|
})
|