gpt4 book ai didi

websocket - 错误 ServerMiddleware 应公开句柄 nuxt

转载 作者:行者123 更新时间:2023-12-02 19:17:13 31 4
gpt4 key购买 nike

  • 新的 nuxt.js 设置不附带服务器文件夹
  • 您创建一个 API 文件夹并在其中放置一个公开服务器的文件
  • 我正在尝试使用 ws 来使用 websockets解析用户 session 并收到此错误的库

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

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'],
}

enter image description here

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

最佳答案

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

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

关于websocket - 错误 ServerMiddleware 应公开句柄 nuxt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63547656/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com