0

If I navigate to http://myurl.com/test I get back on the page Reached me! as I should. However, when I navigate to https://myurl.com/test, chrome says Error: Cannot match any routes. URL Segment: 'test'. All my api/user routes work perfectly on either http or https.

NOTE: When I remove the two app.use lines for Angular, /test then works on both http and https, but obviously my Angular project is then gone :/

server.js

const { mongoose } = require('./database/db');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');

var userController = require('./controllers/userController');

app.use(cors({ origin: '*' }));

const port = process.env.PORT || 3000;

server.listen(port, () => console.log('Server started at port : ' + port));

app.use(bodyParser.json());

app.use('/api/user', userController);

// here lies the client stuff!

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'clientWrapper'));

app.get('/test', (req, res)=>{
  res.send('Reached me!');
})

// This stuff has to be after the app.get stuff or it will override it!
// DOesn't work without this
app.use('/', express.static(path.join(__dirname, 'angular')));

// doesn't let you go straight to URLS without this!
app.use((req, res, next) => {
  res.sendFile(path.join(__dirname, 'angular', 'index.html'));
});

My AWS ports enter image description here

Ryan Soderberg
  • 585
  • 4
  • 21

2 Answers2

0
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();

var options = {
  key: fs.readFileSync('/path/to/key.pem'),
  cert: fs.readFileSync('/path/to/cert.pem'),
  ca: fs.readFileSync('/path/to/ca.pem')
};


https.createServer(options, app).listen(443);

Your application is not listening to https.

Update:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();

http.createServer(app).listen(80);

var options = {
  key: fs.readFileSync('/path/to/key.pem'),
  cert: fs.readFileSync('/path/to/cert.pem'),
  ca: fs.readFileSync('/path/to/ca.pem')
};

https.createServer(options, app).listen(443);
Mushariar
  • 132
  • 2
  • 9
0

Solved it! It's related to Angular Service Workers. Solution here -> Angular 5 and Service Worker: How to exclude a particular path from ngsw-config.json

Ryan Soderberg
  • 585
  • 4
  • 21