私はテスト用の小さなWebサーバーを作成しようとしています。私はNodeJSで作った。しかし予期せぬことが起こった。 NodeJSサーバーが渡したWebページを正しく表示できませんでした。しかし、私がPHP + Apacheを使用したとき、Webページは完全に機能しました。私のクライアント側で受け取ったソースコードを開いたときに、目に見える違いはありません。ここに私のコードです:NodeJSサーバーのResponse.write()または.toString()(bug?)
Server.jsが
var http = require('http');
var fs = require('fs');
var url = require('url');
var Max = 30;
var port = process.argv[2];
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname; if (pathname == "") pathname = "index.html";
console.log("Request for " + pathname + " received.");
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
response.writeHead(404, {'Content-Type': 'text/html'});
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data.toString());
}
response.end();
});
}).listen(port);
console.log('Server running at http://127.0.0.1:8081/');
var sockets = {}, nextSocketId = 0;
server.on('connection', function (socket) {
var socketId = nextSocketId++;
sockets[socketId] = socket;
console.log('socket', socketId, 'opened');
socket.on('close', function() {
console.log('socket', socketId, 'closed');
delete sockets[socketId];
});
socket.setTimeout(4000);
});
function anyOpen(array) {
for (var ele in array) {
if (ele) return true;
}
return false;
}
(function countDown (counter) {
console.log(counter);
if (anyOpen(sockets)) {
return setTimeout(countDown, 1000, Max);
} else if (counter > 0) {
return setTimeout(countDown, 1000, counter - 1);
};
server.close(function() { console.log('Server closed!'); });
for (var socketId in sockets) {
console.log('socket', socketId, 'destroyed');
sockets[socketId].destroy();
}
})(Max);
Chatroom2-0.php
<!DOCTYPE html>
<html>
<head>
<style>
textarea {
width:95%;
rows:50;
height:80%;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script type="text/javascript">
var str = "";
function enter(e){
if (e.keyCode == 13 && document.getElementById("Input").value) {
//alert("Enter!!!!");
sendInput();
document.getElementById("Input").value = "";
}
};
function updateBoard() {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("MsgBoard").innerHTML = xmlhttp.responseText;
}
var textarea = document.getElementById('Output');
textarea.scrollTop = textarea.scrollHeight;
};
xmlhttp.open("POST","Server.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("Type=Username&Content="+document.getElementById("Username").value);
};
function sendInput() {
username = document.getElementById("Username").value; if (!username) username = "Gotemptyname";
msg = document.getElementById("Input").value; if (!msg) msg = "GotNothing";
if (msg) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","Server.php",true);
//xmlhttp.open("POST","test.txt",true);
//xmlhttp.send();
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("Type=Message&Username="+username+"&Content="+msg);
//alert(xmlhttp.responseText);
}
};
</script>
</head>
<body onload="setInterval('updateBoard()',1000)">
<div id="MsgBoard"></div>
<form name="UsrInput">
<?php
if (isset($_POST["Username"]))
echo '<input type="text" id ="Username" value="'.$_POST["Username"].'" disable>';
else {
header("Location: /login/index.html");
die();
}
?>
<input type="text" id="Input" onkeypress="enter(event)" value="" >
</form>
</body>
</html>
ユーザーがログイン後Chatroom2-0.phpにアクセスすることができるはずです。ログイン機能もOKです。しかし、私がChatroom2-0.phpに入ったとき、私はテキストボックスの隣に文字列を持っていました。
'; else {header( "Location:/login/index.html");死ぬ(); }?>
私は文字列がファイル内の私のPHPコードの一部であることに気付きました。私は何が起こっているのか分からない。これは、response.write()またはdata.toString()関数と関係していると思います。関数が私のコーディングで何か変わったのでしょうか?どうすればこの問題を解決できますか?
とにかく、何か助けていただきありがとうございます。
ああ、私は最初に気づいたはずだった(私は今、愚かな気がしている...)。ありがとう、たくさん – FunnyFunkyBuggy
あなたは大歓迎です。あなたが将来何か質問をすることを妨げないように、何かを知らないことについて愚かなことを感じることはありません。 Stack Exchangeコミュニティは、知らないうちに尋ねることを恥ずかしくない人に繁栄しています。求め続けます! – kennasoft