【发布时间】:2019-10-03 04:47:01
【问题描述】:
我有一个工作项目,GraphQLServer 从端口 4000 为我的 react 应用程序提供服务,socket-io 在端口 5000 上侦听。
我正在尝试部署到 heroku,所以我需要它们在同一个端口上(process.env.PORT),但我不知道如何使用 GraphQLServer 来实现。
This is how to do it with express + socket-io
This is the start script for GraphQLServer
Gist with my current code is here
相关代码:
import server from './server';
import express from 'express';
import path from 'path';
const http = require('http').Server(server.express);
const io = require('socket.io')(http);
require('./socket')(io);
server.express.use(express.static(path.join(__dirname, '../../client/build')));
server.express.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, '../../client/build', 'index.html'));
});
// Socket-io listener
http.listen({
port: process.env.PORT || 5000
}, () =>
console.log('listening on port ' + process.env.PORT || 5000)
);
// GraphQL and app listener
server.start({
cors: {
credentials: true,
origin: '/'
}
// port: process.env.PORT || 4000
},
() => {
console.log('The server is up!');
}
);
【问题讨论】:
标签: node.js express heroku socket.io apollo