Configuring a Node.js application to work with Websockets over TCP as well as AWS S3

眉间皱痕 提交于 2020-01-06 05:31:08

问题


I'm working on a Node.js application and installing WebSockets to use with ParseLiveQuery. For the app to work in full, I need HTTPS access for S3, images, etc. and TCP access for the WebSocket.

Below is my current setup:

And my index.js file

var S3Adapter = require('parse-server').S3Adapter;
var s3Adapter = new S3Adapter(
    "myAppMediaServer",
    { directAccess: true,
      baseUrl: 'http://myApp12345.cloudfront.net' // This could be your CloudFront URL
    }
);
var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://name:othername@ds55555-a0.mlab.com:9999,ds02222-a1.mlab.com:44444/appprod?replicaSet=rs-ds019322',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID,
  masterKey: process.env.MASTER_KEY,
  serverURL: process.env.SERVER_URL,
  javascriptKey: process.env.JAVASCRIPT_KEY, vars
  clientKey: process.env.CLIENT_KEY,
  facebookAppIds: ['12345678'],
  filesAdapter: s3Adapter,
  verbose: process.env.VERBOSE_KEY || false,
  allowClientClassCreation: process.env.CLIENT_CREATION || false,
  liveQuery: {
      classNames: ['GroupConvos', 'GroupMessages']
  },
  databaseOptions: { //defaults to 5 and caused bottlenecking issues
    poolSize: 500
  },
  maxUploadSize: "5mb"
});

var app = express();
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});
var parseLiveQueryServer = ParseServer.createLiveQueryServer(httpServer); 

I've been made aware that I need to change my 443 port back to HTTPS from a TCP instance protocol and I'm a bit confused about how to get this working precisely so that my app supports both TCP for sockets and HTTPS for everything else.

Thanks

Edit

Right now I have an HTTP listener on port 80. It sounds like I need to create another server at 443 like the code below. That's step 1, right?

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);

httpServer.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});

// new
var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};

const https = require('https');
var httpsServer = https.createServer(options, app);
httpsServer.listen(443, function() {
    console.log('ParseServer running on port 443 with SSL.');
});
var parseLiveQueryServer = ParseServer.createLiveQueryServer(httpsServer);

Then what is the configuration on my AWS load balancer to allow for HTTPS connections (so that S3 file get/post) works and what is the secure TCP configuration such that I'm using wss to run my socket?


回答1:


In a simple example you need to do something like this. I am showing the Let's Encrypt style of SSL certificates, which I do recommend. I also recommend just using HTTPS today and not using HTTP at all (or just redirect HTTP to HTTPS).

var https = require('https')

var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};

var httpsServer = require('https').createServer(options,app);

httpsServer.listen(443, function() {
    console.log('ParseServer running on port 443 with SSL.');
});

var parseLiveQueryServerSecure = ParseServer.createLiveQueryServer(httpsServer); 


来源:https://stackoverflow.com/questions/51885382/configuring-a-node-js-application-to-work-with-websockets-over-tcp-as-well-as-aw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!