I need to make a simple website,that contains header(needs to be in a different file) and main part.All of that should be running on server.
App.js
const fs = require("fs")
const http = require("http")
let server = http.createServer((req, res) => {
res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"})
if(req.url == "/"){
fs.readFile("ultimate.html", "utf-8", function(error, data){
console.log("Loaded") //confirms the loading of a page
res.end(data)
})
}
})
const PORT = 3000
const HOST = "localhost"
server.listen(PORT, HOST, () => {
console.log(`Server is up: http://${HOST}:${PORT}`)
})
ultimate.html
<html>
</head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(req,res){
console.log(status)
$("#header").html('Did i disappear?'); //This message is present on the screen
$("#header").load("Text.txt"); //This thing doesnt load at all
});
</script>
</head>
<body>
<div id="header"></div>
</body>
</html>
I tried loading a small txt file and a html file as a header, but nothing works. The only thing that works is .html but that’s about it.Also console log (status) has a blank value.
Im using NODEJS if thats helps
>Solution :
Is the code you have given complete ? As you can read in the .load documentation, $.load('Text.txt') will act in the same as $.get(Text.txt). It will execute a request to the /Text.txt path.
On the server side, nothing seems ready to handle this. You should add a route to Text.txt that loads the file and return its content.
You can see this for yourself by opening your browser devtools and going to « network » tab. You should see a request to Text.txt, resulting with a 404 status.
Your console.log has a blank value because the status variable is undefined.