0

I'm dealing with SSL/https on my server.

I configured two ports, one for HTTP (4000) and another for HTTPS (9080) using iptables:

$ sudo iptables -t natL -n --line-number 1 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 redir ports 9080 2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 4000 

How can I tell Parse Server to redirects all traffic coming http to use https?

I did previously a similar configuration using expressJS but it is not working with Parse Server, this is the code I previously used:

app.use(function (req, res, next) { return res.redirect(['https://domainName.tv', req.url].join('')); }); 

Here is my app index.js

var express = require('express'); var ParseServer = require('parse-server').ParseServer; var path = require('path'); var bodyParser = require('body-parser'); var SimpleSendGridAdapter = require('parse-server-sendgrid-adapter'); var stripe = require("stripe"); var https = require('https'); var http = require('http'); var fs = require('fs'); var databaseUri = 'mongodb://localhost:27017/dev'; if (!databaseUri) { console.log('DATABASE_URI not specified, falling back to localhost.'); } var options = { key: fs.readFileSync(__dirname + '/ssl/domainName.key'), cert: fs.readFileSync(__dirname + '/ssl/domainName.crt') }; var api = new ParseServer({ databaseURI: databaseUri || 'mongodb://localhost:27017/dev', cloud: __dirname + '/cloud/main.js', appId: 'xN6xxxxxxxxxxxxxxxxmx', masterKey: 'egxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdn', javascriptKey: 'O2xxxxxxxxxxxxxxxxxxxxxxxxxYy', //Add your master key here. Keep it secret! serverURL: 'https://localhost:9080/parse', restAPIKey: 'lxxxxxxxxxxxxxxxN', appName: 'domainName.TV', publicServerURL: 'https://domainName.tv/parse', emailAdapter: SimpleSendGridAdapter({ apiKey: 'SG.SbxxxxxxxxxxxxxxxxxxxxxxxxxxxxMxc', fromAddress: '[email protected]', }), liveQuery: { classNames: ["UsPrIn", "Event"] // List of classes to support for query subscriptions } }); var app = express(); // redirect all http requests to https // THIS IS NOT WORKING!!!! app.use(function (req, res, next) { return res.redirect(['https://domainName.tv', req.url].join('')); }); // Serve static assets from the /public folder app.use('/public', express.static(path.join(__dirname, '/public'))); // Serve the Parse API on the /parse URL prefix var mountPath = process.env.PARSE_MOUNT || '/parse'; app.use(mountPath, api); app.get('/*', function(req, res) { res.sendFile(path.join(__dirname, '/public/index.html')); }); var port = process.env.PORT || 9080; var httpsServer = https.createServer(options, app).listen(port, function() { console.log('parse-server running on SSL port ' + port + '.'); }); var httpServer = http.createServer(app).listen(4000, function() { console.log('parse-server running on port 4000.'); }); // This will enable the Live Query real-time server ParseServer.createLiveQueryServer(httpsServer); 

My solutions is similar to this one but I'm using Parse Server: https://stackoverflow.com/questions/7450940/automatic-https-connection-redirect-with-node-js-express

1 Answer 1

-1

I found the answer in case someone is in need: req.secure

this is the code:

app.use(function (req, res, next) { if(!req.secure && (req.headers.host !== 'localhost:7777')) { return res.redirect(['https://mydomain.tv', req.url].join('')); } next(); }); 
1
  • Redirect should be PERMANENT REDIRECT, so next time client tries to connect in the same session, it will immediately try with HTTPS. Redirect like this: res.redirect(301, 'https://mydomain.tv'+req.url). Commented Oct 5, 2017 at 11:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.