【发布时间】:2020-07-10 22:23:21
【问题描述】:
我正在使用 Vue 实现一个 Web 应用程序。在应用程序中,我使用“推送器”来创建实时多用户通信。特别是,我设置了一个 node.js 服务器,侦听本地网络中特定设备的端口 5000。该应用程序在我的家庭网络中运行(我能够与连接的不同设备进行交互)。现在我想在 Firebase 上部署应用程序。
下面是与 pusher 实例的定义相关的代码,“authEndpoint”属性指定用户必须发送 http post 请求的路径才能订阅共享频道。当我在 firebase 上部署应用程序时,我应该如何管理 authEndpoint 属性和 node.js 服务器的位置,以使应用程序在互联网上运行?
import Pusher from 'pusher-js'
const pusher = new Pusher('*************', {
cluster: 'eu',
encrypted: true,
authEndpoint: '192.168.1.237:5000/pusher/auth'
})
这是与服务器相关的代码:
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const app = express()
const Pusher = require('pusher')
const crypto = require('crypto')
const cors = require('cors')
//initialize Pusher with your appId, key and secret
const pusher = new Pusher({
appId: '****',
key: '**************',
secret: '************',
cluster: 'eu',
encrypted: true
})
// Body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors())
// The code below helps to fix any potential CORS issue.
app.use((req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*')
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true)
// Pass to next layer of middleware
next()
})
// Index API route for the Express app
app.get('/', (req, res) => {
res.send('Welcome')
})
// API route used by Pusher as a way of authenticating users
app.post('/pusher/auth', (req, res) => {
let socketId = req.body.socket_id
let channel = req.body.channel_name
// Generate a random string and use as presence channel user_id
let presenceData = {
user_id: crypto.randomBytes(16).toString('hex')
}
let auth = pusher.authenticate(socketId, channel, presenceData)
res.send(auth)
})
// Set port to be used by Node.js
app.set('port', (5000))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
【问题讨论】:
-
我不确定我是否理解这个问题。 stackoverflow.com/help/how-to-ask
-
我无法让应用程序在互联网上运行。特别是当我在 Firebase 上部署应用程序时,我不知道在哪里放置 node.js 服务器,该服务器管理对共享推送器通道的订阅(用于在用户之间共享信息)。在我的家庭网络中,node.js 服务器正在监听 192.168.1.237:5000。 @AlexMA
标签: javascript node.js firebase vue.js pusher