错误 ServerMiddleware 应该公开一个句柄 nuxt

Posted

技术标签:

【中文标题】错误 ServerMiddleware 应该公开一个句柄 nuxt【英文标题】:Error ServerMiddleware should expose a handle nuxt 【发布时间】:2020-12-12 07:17:33 【问题描述】: 新的 nuxt.js 设置不附带服务器文件夹 您创建一个 API 文件夹并在其中放置一个文件以公开服务器 我正在尝试通过ws 库使用 websockets 来解析用户会话并收到此错误

这是我的 app.js 代码放在 api 文件夹中

import http from 'http'
import logger from 'express-pino-logger'
import express from 'express'
import cookieParser from 'cookie-parser'
import WebSocket from 'ws'
const app = express()
const sessionParser = cookieParser()
const map = new Map()
app.use(logger())
app.use(express.json())
app.use(express.urlencoded( extended: true ))
app.use(sessionParser)
app.use('/v1', (req, res) => res.json('hello'))

const server = http.createServer(app)
const wss = new WebSocket.Server( noServer: true )

wss.on('connection', function connection(ws, request, client) 
  ws.on('message', function message(msg) 
    console.log(`Received message $msg from user $client`)
  )
)

server.on('upgrade', function (request, socket, head) 
  console.log('Parsing session from request...')

  sessionParser(request, , () => 
    if (!request.session.userId) 
      socket.destroy()
      return
    

    console.log('Session is parsed!')

    wss.handleUpgrade(request, socket, head, function (ws) 
      wss.emit('connection', ws, request)
    )
  )
)

wss.on('connection', function (ws, request) 
  const userId = request.session.userId

  map.set(userId, ws)

  ws.on('message', function (message) 
    //
    // Here we can now use session parameters.
    //
    console.log(`Received message $message from user $userId`)
  )

  ws.on('close', function () 
    map.delete(userId)
  )
)

server.listen(3000)

export default server

我的 nuxt.config.js 文件

export default 
  /*
   ** Nuxt rendering mode
   ** See https://nuxtjs.org/api/configuration-mode
   */
  mode: 'universal',
  /*
   ** Nuxt target
   ** See https://nuxtjs.org/api/configuration-target
   */
  target: 'server',
  /*
   ** Headers of the page
   ** See https://nuxtjs.org/api/configuration-head
   */
  head: 
    title: process.env.npm_package_name || '',
    meta: [
       charset: 'utf-8' ,
       name: 'viewport', content: 'width=device-width, initial-scale=1' ,
      
        hid: 'description',
        name: 'description',
        content: process.env.npm_package_description || '',
      ,
    ],
    link: [ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' ],
  ,
  /*
   ** Global CSS
   */
  css: [],
  /*
   ** Plugins to load before mounting the App
   ** https://nuxtjs.org/guide/plugins
   */
  plugins: [],
  /*
   ** Auto import components
   ** See https://nuxtjs.org/api/configuration-components
   */
  components: true,
  /*
   ** Nuxt.js dev-modules
   */
  buildModules: [
    // Doc: https://github.com/nuxt-community/eslint-module
    '@nuxtjs/eslint-module',
    // Doc: https://github.com/nuxt-community/stylelint-module
    '@nuxtjs/stylelint-module',
  ],
  /*
   ** Nuxt.js modules
   */
  modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    '@nuxtjs/pwa',
  ],
  /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
  axios: ,
  /*
   ** Build configuration
   ** See https://nuxtjs.org/api/configuration-build/
   */
  build: ,
  serverMiddleware: ['~/api/app'],

如何从 app.js 公开 websocket 服务器?

【问题讨论】:

在发现 nuxt 必须自己调用它之后尝试删除 server.listen(3000) 仍然是同样的错误 【参考方案1】:

你应该导出你的服务器中间件。在底部添加:

// 导出服务器中间件 模块.exports = 路径:'/api', 处理程序:应用程序

【讨论】:

以上是关于错误 ServerMiddleware 应该公开一个句柄 nuxt的主要内容,如果未能解决你的问题,请参考以下文章

Nuxt serverMiddleware 从 API 获取 json

如何使用 Nuxt ServerMiddleware 和 Apollo GraphQL 处理 301 重定向

为啥片段类应该是公开的?

我应该等待多长时间才能公开免费/开源项目中的漏洞? [关闭]

错误 AADSTS50012:客户端是公开的,因此不应显示“client_assertion”

不为其关系公开外键属性的实体出错