今天依然重复写个简单的web server例子,但是在fs.exists判断上出了问题。代码如下:
http.createServer(function(req, res){
var reqPath = url.parse(req.url).pathname;
fs.exists(reqPath, function(exists){
if(!exists){
consloe.log(reaPath + ' not exists.');
}else{
//do something
}
});
});
在浏览器请求http://localhost:8888/index.html 时,console输出的信息居然是 “/index.html not exists.”。 首先,index.html是存在的,并且与server.js同在一个目录下。
目录结构如下: http/ |-server.js |-index.html
当我把if判断改成这样,就可以顺利执行do something了 if(exists){ console.log(reqPath + ' not exists.'); }else{ //do something }
按理说,文件存在,exists返回的是true,否则返回false。但为什么我这里的逻辑居然是相反的?