// сервер. js
Это мой node.js сервер. Я пытаюсь понять узел и его базовую функциональность, прежде чем отправиться в express. Кроме того, бэкэнд вообще смущает меня.
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
if(req.url === '/'){
fs.readFile('home.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}
});
server.listen(3000);
console.log("listening on 3000");
// home. html
basi c html file. Содержит кнопку. и после нажатия кнопки я намерен напечатать файл sample.txt. Я нажимаю кнопку и образец.txt успешно переходит во вкладку сети. но по какой-то причине не отобразит содержимое в окне консоли.
<!DOCTYPE html>
<html>
<head>
<title>
Little structure
</title>
</head>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "sample.txt", true);
xhttp.onload = function() {
if (this.status == 200) {
console.log(this.responseText);
}
};
xhttp.send();
}
</script>
</body>
</html>
// sample.txt
ПРИВЕТ ОТ SAMPLE.TEXT