-2

My webpage creates a new script element and then loads the script from a localhost server. The problem is that the script cannot be loaded and shows PROTOCOL ERROR:

https://localhost:4200/script/test.js net::ERR_SSL_PROTOCOL_ERROR

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link href="./public/nps.style.css" rel="stylesheet" />
        <script>
          j = document.getElementsByTagName('script')[0],
          k = document.createElement('script');
          k.src = 'http://localhost:4200/script/test.js';
          j.parentNode.insertBefore(k,j);
        </script>
  </head>
  <body>
    <p>Testing</p>
  </body>
</html>

Then create a localhost server to listen it so the JavaScript file can be downloaded

index.js:

const express = require('express');
const cors = require('cors');
const { test } = require('./script/test.js');

const api = express();
api.use(cors({origin: '*'}));
api.use('/script', express.static('public'));

api.listen(4200, () => {
  console.log("Server listening");
})

test.js: alert("it works!");

How can I solve this issue?

Update:

Initially I had the source of the new script element as 'https://localhost:4200/script/test.js', So I changed to http, a new error occurred:

GET http://localhost:4200/script/test.js net::ERR_ABORTED 404 (Not Found)
0

2 Answers 2

1

Have you considered not using https? It looks like this could be your issue running locally. The protocol error is the giveaway.

Sign up to request clarification or add additional context in comments.

Comments

0

You just change 2 lines of your index.js code to solve it. Try with this code :

const express = require('express');
const cors = require('cors');
// const { test } = require('./script/test.js');

const api = express();
api.use(cors({ origin: '*' }));
api.use(express.static('public'));
api.use('/script', express.static('script'));

api.listen(4200, () => {
    console.log("Server listening");
});

And make sure your directory/file management looks like this :

file management

1 Comment

I’m just curious how this changes anything because he is getting a protocol error which means that he’s trying to hit HTTPS on the local machine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.