content: Websocket error using alternative server framework

Version

@nuxt/content: 1.3.2 nuxt: 2.12.2

Reproduction Link

https://github.com/mikeburgh/nuxt-hapi-content Created with npx create-nuxt-app -selecting hapi as the server framework -then adding in the @nuxt/content module

Steps to reproduce

Clone repo Run npm run dev Browse to : http://localhost:3000

What is Expected?

No errors in the console

What is actually happening?

Console is showing an error

 ERROR  Cannot read property 'length' of undefined                                                                                                                                                                20:57:36

  at WebSocket.setSocket (node_modules/@nuxt/content/node_modules/ws/lib/websocket.js:165:14)
  at WebSocketServer.completeUpgrade (node_modules/@nuxt/content/node_modules/ws/lib/websocket-server.js:322:8)
  at WebSocketServer.handleUpgrade (node_modules/@nuxt/content/node_modules/ws/lib/websocket-server.js:245:10)
  at WS.handleUpgrade (node_modules/@nuxt/content/lib/ws.js:21:21)
  at WS.serve (node_modules/@nuxt/content/lib/ws.js:17:10)
  at node_modules/@nuxt/content/lib/middleware.js:11:15
  at call (node_modules/connect/index.js:239:7)
  at next (node_modules/connect/index.js:183:5)
  at next (node_modules/connect/index.js:161:14)
  at WebpackBundler.middleware (node_modules/@nuxt/webpack/dist/webpack.js:5410:5) (repeated 1 times)

The error seems to be caused by the serve method in @nuxt/content/lib/ws.js and happens with both Hapi and express frameworks.

serve (req) {
    this.handleUpgrade(req, req.socket, undefined)
  }

Changing the undefined to an empty array solves it, although not sure if head should actually be something ? Happy to send a PR if head just needs to be an empty array ?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 16 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I have a similar problem

 ERROR  Cannot read property 'toLowerCase' of undefined                   

  at WebSocketServer.handleUpgrade (node_modules/ws/lib/websocket-server.js:192:27)
  at WS.handleUpgrade (node_modules/@nuxt/content/lib/ws.js:21:21)
  at WS.serve (node_modules/@nuxt/content/lib/ws.js:17:10)
  at node_modules/@nuxt/content/lib/middleware.js:12:15
  at call (node_modules/connect/index.js:239:7)
  at next (node_modules/connect/index.js:183:5)
  at next (node_modules/connect/index.js:161:14)
  at WebpackBundler.middleware (node_modules/@nuxt/webpack/dist/webpack.js:5532:5)

I have a similar problem

 ERROR  Cannot read property 'toLowerCase' of undefined                   

  at WebSocketServer.handleUpgrade (node_modules/ws/lib/websocket-server.js:192:27)
  at WS.handleUpgrade (node_modules/@nuxt/content/lib/ws.js:21:21)
  at WS.serve (node_modules/@nuxt/content/lib/ws.js:17:10)
  at node_modules/@nuxt/content/lib/middleware.js:12:15
  at call (node_modules/connect/index.js:239:7)
  at next (node_modules/connect/index.js:183:5)
  at next (node_modules/connect/index.js:161:14)
  at WebpackBundler.middleware (node_modules/@nuxt/webpack/dist/webpack.js:5532:5)

I’m having that issue without an alternative server framework. I’m using nuxt defaults

I had similar problem. I was running nuxt locally behind nginx reverse proxy and when I enabled websockets in nginx, problem disappeared. Maybe it will help somebody.

I think I’ve managed to fix this error with the following:

import http from 'http'
import { loadNuxt, build } from 'nuxt'
import express from 'express'

const app = express()
const port = 3000
const isDev = process.env.NODE_ENV !== 'production'

export const listen = async () => {
  const nuxt = await loadNuxt(isDev ? 'dev' : 'start')
  app.use(nuxt.render)
  if (isDev) {
    build(nuxt)
  }
  const server = http.createServer(app)
  server.listen(port, '0.0.0.0', async () => {
    await nuxt.callHook('listen', server)
    // eslint-disable-next-line no-console
    console.log(`Server listening on port ${port} .`)
  })
}