简体   繁体   English

express app.get 在 HTTP 但不是 HTTPS

[英]express app.get working over HTTP but not HTTPS

If I navigate to http://myurl.com/test I get back on the page Reached me!如果我导航到http://myurl.com/test我会回到Reached me! as I should.正如我应该的那样。 However, when I navigate to https://myurl.com/test , chrome says Error: Cannot match any routes. URL Segment: 'test'但是,当我导航到https://myurl.com/test时,chrome 显示Error: Cannot match any routes. URL Segment: 'test' Error: Cannot match any routes. URL Segment: 'test' . Error: Cannot match any routes. URL Segment: 'test' All my api/user routes work perfectly on either http or https.我所有的api/user路由都可以在 http 或 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:/ 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我的 AWS 端口在此处输入图像描述

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.您的应用程序未收听 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);

Solved it.解决了。 It's related to Angular Service Workers.它与 Angular Service Workers 有关。 Solution here -> Angular 5 and Service Worker: How to exclude a particular path from ngsw-config.json此处的解决方案-> Angular 5 和 Service Worker:如何从 ngsw-config.json 中排除特定路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM