部署使用两个端口的应用程序
Deploy an application that uses two ports
我正在使用 Vue 实现一个网络应用程序。在我的应用程序中,我使用 Pusher 来实现实时多用户通信。
在本地,该应用程序在“http://loocalhost:8080" and I set up a node.js server on "http://localhost:5000”上运行,该应用程序管理推送者共享频道上的用户订阅以及他们之间的事件共享。该应用程序工作正常。现在我想将它部署在托管服务器上,使其在 Internet 上运行。
我的问题与需要使用两个不同的端口有关。特别是我不知道如何组织我的 "server.js" 文件来执行此操作(或者如果像 Heroku 这样的平台可以支持使用两个端口)。下面我 post 我的实际 'server.js'文件。希望你能帮助我。
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',
useTLS: 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', false)
// 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'))
})
您正在使用 Vue,因此在开发中,前端由 Webpack 的 devServer 在端口 8080 上在本地提供服务。您也是 运行 正在开发中的节点服务器,它模拟 API生产服务器的一部分。
在实际生产中,不需要2台服务器或2个端口。在生产中,您的前端不需要专用服务器。那只是为了快速方便的开发。让节点生产服务器也为前端服务。像这样取决于你的服务器结构:
// Static assets
app.use(express.static(path.join(__dirname, 'dist')));
Heroku 不支持多个端口,您也不需要它们。从一个域同时为后端和前端提供服务是可能的(而且是正常的)。
我正在使用 Vue 实现一个网络应用程序。在我的应用程序中,我使用 Pusher 来实现实时多用户通信。
在本地,该应用程序在“http://loocalhost:8080" and I set up a node.js server on "http://localhost:5000”上运行,该应用程序管理推送者共享频道上的用户订阅以及他们之间的事件共享。该应用程序工作正常。现在我想将它部署在托管服务器上,使其在 Internet 上运行。
我的问题与需要使用两个不同的端口有关。特别是我不知道如何组织我的 "server.js" 文件来执行此操作(或者如果像 Heroku 这样的平台可以支持使用两个端口)。下面我 post 我的实际 'server.js'文件。希望你能帮助我。
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',
useTLS: 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', false)
// 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'))
})
您正在使用 Vue,因此在开发中,前端由 Webpack 的 devServer 在端口 8080 上在本地提供服务。您也是 运行 正在开发中的节点服务器,它模拟 API生产服务器的一部分。
在实际生产中,不需要2台服务器或2个端口。在生产中,您的前端不需要专用服务器。那只是为了快速方便的开发。让节点生产服务器也为前端服务。像这样取决于你的服务器结构:
// Static assets
app.use(express.static(path.join(__dirname, 'dist')));
Heroku 不支持多个端口,您也不需要它们。从一个域同时为后端和前端提供服务是可能的(而且是正常的)。