I have hosted an express.js app on amazon ec2 instance using node index.js
. In my index.html
I have following 2 lines in <head>
<script type="module" src="https://51.20.7.73:5000/buildjs.js"></script>
<link rel="stylesheet" href="http://51.20.7.73:5000/buildcss.css">
But the problem is that when this HTML is rendered in chrome browser, these requests are automatically converted to https instead of http which is giving error in the console as follows
My page is itself hosted to http : http://51.20.7.73:5000/
and when I separately open http://51.20.7.73:5000/buildjs.js
in browser, it is serving the file correctly.
I don’t know why are these requests transferred to HTTPS automatically, but due to this my react page is not working, Can someone please help? Thanks!
2
Could this be kind of a duplicate of this question?
https://superuser.com/questions/565409/how-to-stop-an-automatic-redirect-from-http-to-https-in-chrome
Or to be more precise, do you want to disable the https conversion in Chrome or adapt your code that it can handle http and https?
1
You will have to use helmet
for express to prevent the hsts
related headers that instructs the browser to use HTTPS
for everything.
const express = require('express');
const helmet = require('helmet');
const app = express();
// Use Helmet to set various HTTP headers for security, except HSTS
app.use(helmet({
hsts: false // Disable HSTS
}));
// Define your routes
app.get('/', (req, res) => {
res.send('Hello, world!');
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});