Hello everyone, I am currently trying to get a webserver running on my Mega2560.
The server itself works quite fine, but over the course of handling multiple GET requests it fails.
When opening the Webpage the server recieves a GET request for the '.../' directory on which the server sends a response by sending a html file.
In such file there is script
Tag refering to the app.js file on which the server receives the next GET request asking for that file. Until here more or less everything is stable and works just fine.
But when sending the contents of named .js file only the functions are visible on the page.
This means, that all global variables, that may be used inside some functions, will not be defined inside the javascript on the browsers side.
I have written a test szenario which functions exactly like the problem with my actual files:
The html file:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link id="favicon" rel="icon" type="image/png" href="Favicons/MinhRP2.jpg" sizes="32x32">
<title>Get some Animals</title>
<style>
h1{
text-align: center;
}
.centered-button{
margin:auto;
display:block;
}
</style>
</head>
<body>
<h1>Testpage</h1>
<div class="button-section">
<button onclick="openAlert()">Open Alert</button>
</div>
<div class="button-section">
<button onclick="openCrash()">Open Crash</button>
</div>
<script src="testCode.js">
</script>
</body>
</html>
The Javascript code that the server writes as response:
// This code will not be shown when send from Arduino Mega2560
var unseenVar = 'I will not be seen on the browsers source code :3'
function openAlert(){
window.alert("This function will be uploaded onto the browser")
}
function openCrash(){
// This will throw an error, due to the 'unseenVar' not being defined
window.alert("But this variable will not: " + unseenVar)
}
Javascript code visible from the source view of the browser:
function openAlert(){
window.alert("This function will be uploaded onto the browser")
}
function openCrash(){
// This will throw an error, due to the 'unseenVar' not being defined
window.alert("But this variable will not: " + unseenVar)
}
Error message inside the console of the browser when pressing the 'Open Crash' button:
Uncaught ReferenceError: unseenVar is not defined
Last but not least the code which sends each response from server site:
void sendFileContent(EthernetClient *client, String* filename){
File file = SD.open(*filename);
// Send confirmation
client->println("HTTP/1.1 200 OK");
if(file){
// Send the Content-Type the file has to be interpreted in
client->println("Content-Type: " + getContentType(filename));
// Send every single character (due to dynamic storage shortage)
while(file.available()){
if(!client->write(static_cast<char>(file.read()))){
Serial.println("Dropped something there!!!!!!!!");
}
}
}
else{
client->print("404 File was not able to load gomenasaiiiiii");
Serial.println("IOException: could not open " + *filename + "!!!!");
}
}
String getContentType(String *filename){
if(filename->endsWith(".htm")){
return "text/html";
}
else if(filename->endsWith(".js")){
return "text/javascript";
}
else if(filename->endsWith(".jpg")){
return "image/jpg";
}
else if(filename->endsWith(".png")){
return "image/png";
}
else{
return "";
}
}
/*
* After sending the File a short delay happens, and client.close() will be called
*/
I am fairly new to the HTTP-Webserver subject on Arduino (have some little experience with node.js). Therefore I have absolutally no Idea where I could have made a mistake on telling the Browser which filecontent he has to interpretate in which way.
But in my head, it makes no sence that any part of the javascript file gets treated differently from other parts ( here the functions) due to the fact that I have clearly told that following file will be written in text and has to be interpreted as javascript code.