File structure
index.js:
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
chat.ejs
<body>
<h1><%= (roomName) %></h1>
<div id="video-grid">
</div>
<script src="script.js"></script>
</body>
while loading, error displayed
"The resource from “http://localhost:5000/chat/Aishu%20study/Aneesa/script.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)."
It works if I keep everything in the same html file. can someone help me with this? I'm not sure what the problem is
File structure
index.js:
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
chat.ejs
<body>
<h1><%= (roomName) %></h1>
<div id="video-grid">
</div>
<script src="script.js"></script>
</body>
while loading, error displayed
"The resource from “http://localhost:5000/chat/Aishu%20study/Aneesa/script.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)."
It works if I keep everything in the same html file. can someone help me with this? I'm not sure what the problem is
Share Improve this question edited Sep 24, 2020 at 7:05 kiranvj 34.2k8 gold badges75 silver badges79 bronze badges asked Sep 24, 2020 at 6:53 AneesaAneesa 411 gold badge1 silver badge3 bronze badges 2-
is
script.js
really a javascript file? also how e error message mentions some/xyz/
subdirectory in the url path, what most likely happened is that the path is incorrect and it matches some other general endpoint returning an html file you did not post in your answer – Krzysztof Krzeszewski Commented Sep 24, 2020 at 6:57 - this is the error message "The resource from “localhost:5000/chat/Aishu%20study/Aneesa/script.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)." – Aneesa Commented Sep 24, 2020 at 6:59
2 Answers
Reset to default 2You should include Your public path like this:
app.use(express.static(__dirname+ '/public'));
And then implement your scripts and styles like this:
<script src="/script.js">
The issue lays in incorrect pathing in your application, you see the way you included script.js in the html file may have been correct if the paths were relative to the same folder. To be precise the ways you could define it are
The one you used "script.js" file is located in the same folder as the current page, it does not work the way you want it, since you are accessing the script tag from the localhost:5000/chat/Aishu%20study/Aneesa/xx
url
<script src="script.js">
The "script.js" file is located in the scripts folder in the current folder
<script src="scripts/script.js">
The "script.js" file is located in the scripts folder at the root of the current web
<script src="/scripts/script.js">
The "script.js" file is located in the folder one level up from the current folder
<script src="../script.js">
And lastly the one you should be using, the "script.js" file is located at the root of the current web
<script src="/script.js">