Got a weird issue. I made simple web-server on nodeJS that serves 3 files (HTML, JS script file and JSON). First 2 files can be found here: https://jsfiddle.net/xyaxn9Lp/ .
JSON contains two properties:
{
"organization" : "ORG1",
"address" : "ORG1 Address"
}
Server code:
const http = require('http');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer(function (req, resp) {
if (req.url === "/") {
fs.readFile("index.html", function (error, pgResp) {
if (error) {
resp.writeHead(404);
resp.write('Contents you are looking are Not Found');
} else {
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp);
}
resp.end();
});
}
else if (req.url === "/main.js") {
fs.readFile("main.js", function (error, pgResp) {
if (error) {
resp.writeHead(404);
resp.write('Contents you are looking are Not Found');
} else {
resp.writeHead(200, { 'Content-Type': 'application/javascript' });
resp.write(pgResp);
}
resp.end();
});
}
else if (req.url === "/organizations.json") {
fs.readFile("organizations.json", function (error, pgResp) {
if (error) {
resp.writeHead(404);
resp.write('Contents you are looking are Not Found');
} else {
resp.writeHead(200, { 'Content-Type': 'application/json' });
resp.write(pgResp);
}
resp.end();
});
}
else {
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write('<h1>Wrong URL!</h1>' + req.url);
resp.end();
}
});
server.listen(port, hostname);
console.log("Server running at http://" + hostname + ":" + port + "/"
The reason i'm doing this - i want two properties of JSON file to be inserted in <h1>
and <p>
tags on empty page. When index.html page is loaded it sends request to my web-server to get this JSON file, and with the usage of createElement
and appendChild
it should add ORG1 to <h1>
and ORG1 Address to <p>
tags.
But i can't get it working! For some reason response from server is empty and all i have is this error:
If i put console.log(organizations)
inside onload function i clearly can see my JSON file is here!