var http = require('http'),
fs=require('fs'),
path=require('path'),
host ='127.0.0.1',
port='9001';
var mimes = {
".htm":"text/html",
".css":"text/css",
".js":"text/javascript",
".gif":"text/gif",
".jpg":"text/jpeg",
".png":"text/png"
}
var server = http.createServer(function(req,res){
var filepath =(req.url==='/')?('./index.htm'):('.'+req.url);
var contentType = mimes[path.extname(filepath)];
//check file exists or not
fs.exists(filepath,function(file_exists){
if(file_exists)
{
res.writeHead(200, {'Content-Type' : contentType});
var streamFile = fs.createReadStream(filepath).pipe(res);
streamFile.on('error',function(){
res.writeHead(500);
res.end();
})
}
else
{
res.writeHead(404);
res.end("sorry we could not find the file you requested");
}
})
}).listen(port,host,function(){
console.log('server running on http://'+host+':'+port);
})
В приведенном выше коде мой сервер узла дает сообщение его прогонов в 127.0.0.1:9001, но когда я запускаю его в моем браузере я сталкиваюсь «ошибка 404 файл не найден» , что не является той ошибкой, о которой я упоминал. Кроме того, у меня есть файл index.html внутри той же папки этого файла сценария. Что случилось с моим кодом?node.js 404 файла не найдена ошибка
Большое спасибо Mahesh :) Это сработало. большое спасибо –