ノードjsは初めてです。リンクをたどっています。しかし、これは常に実行されているため、 index.htmlresponse.writeHead(404);
を表示できません
これは私の Web サーバー ノードの js コードです。
var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = "."+request.url;
if (filePath == './')
filePath = '/index.html';
else
console.log(request.url);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
fs.exists( filePath, function (exists) {
console.log(filePath);
if (exists) {
fs.readFile(filePath, function (error, content) {
if (error) {
console.log(error);
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(8125);
私の質問。
- これは index.html を表示していません。
- 応答または html ファイル ベースのさまざまな要求を送信するにはどうすればよいですか? 例:
http://localhost:8125
-> index.htmlhttp://localhost:8125/firstpage.html
-> firstpage.html - server.js (Web サーバー) を含む同じディレクトリに html ファイルを配置する必要がありますか?
cloud9 ideでも同じことを試しました。
SOで同様の問題を見つけました。しかし、それを修正する方法が明確にわかりませんでした。 Node.js - 基本ノード Web サーバーによって提供されていない Socket.io クライアント ファイル よろしくお願いします 。