0
私はノードJSを初めて使い、自分のWebサーバを作成して、自分のLinuxサーバ(Raspberry Pi)に自分の非常に基本的なWebサイトをホストしようとしています。次のコードはindex.htmlを表示しますが、users.htmlファイルは表示しません。私が間違っていることを指摘してください。このフォルダにindex.htmlファイルとusers.htmlファイルがあります。 (Sidenote:私はCSS &がまだ実装されていないことを知っています;私はこのWebサーバーを書く冒頭にあります)。ノードjs Webサーバーが余分なページで動作しないのはなぜですか?
var http = require('http');
var fs = require('fs');
//404 response
function send404Response(response){
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("Error 404: Page not found!");
response.end();
}
//Handles user request
function onRequest(request, response){
if(request.method == 'GET' && request.url == '/'){
response.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream("./index.html").pipe(response);
}else if(request.method == 'Get' && request.url == '/users'){
response.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream("./users.html").pipe(response);
}
else{
send404Response(response);
}
}
http.createServer(onRequest).listen(8884);
console.log("Server is running...");
'request.method == 'Get'' - すべての大文字で' GET'する必要があります。 – brandonscript