【问题请教】Nodejs中关于fs.exists的判断问题
【问题请教】Nodejs中关于fs.exists的判断问题
今天依然重复写个简单的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。但为什么我这里的逻辑居然是相反的?
【问题请教】Nodejs中关于fs.exists的判断问题
今天依然重复写个简单的web server例子,但是在fs.exists
判断上出了问题。代码如下:
http.createServer(function(req, res) {
var reqPath = url.parse(req.url).pathname;
fs.exists(reqPath, function(exists) {
if (!exists) {
console.log(reqPath + ' not exists.');
} else {
// do something
}
});
});
在浏览器请求 http://localhost:8888/index.html
时,console
输出的信息居然是 "index.html not exists."
。
首先,index.html
是存在的,并且与 server.js
同在一个目录下。
目录结构如下:
http/
|- server.js
|- index.html
问题分析
根据你的描述,你期望的是当文件存在时,执行某些操作;而当文件不存在时,输出错误信息。然而,你的代码逻辑似乎颠倒了。这是因为 fs.exists
的回调函数中的 exists
参数表示文件是否不存在。如果文件不存在,exists
为 false
;如果文件存在,exists
为 true
。
解决方案
为了使代码逻辑正确,你需要调整条件判断,确保在文件存在时执行某些操作,在文件不存在时输出错误信息。以下是修改后的代码示例:
const http = require('http');
const url = require('url');
const fs = require('fs');
http.createServer(function(req, res) {
var reqPath = url.parse(req.url).pathname;
fs.exists(reqPath, function(exists) {
if (exists) {
console.log(reqPath + ' exists.');
// do something, e.g., read file content or serve the file
} else {
console.log(reqPath + ' not exists.');
// handle 404 error
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
解释
- 引入模块:我们引入了
http
,url
和fs
模块。 - 创建服务器:使用
http.createServer
创建一个 HTTP 服务器。 - 解析请求路径:通过
url.parse(req.url).pathname
获取请求路径。 - 检查文件是否存在:使用
fs.exists
检查文件是否存在,并在回调函数中处理逻辑。 - 调整条件判断:将条件判断改为
if (exists)
,这样当文件存在时会输出exists
并执行某些操作;当文件不存在时会输出not exists
并返回 404 错误。
希望这个解决方案能帮助你解决问题!
你的req.path
是/index.html
,而你的文件是在server.js
同一个目录下的…你用fs.read('/index.html', callback)
会去读根目录下的index.html
文件,当然会不存在。
目录结构如下: http/ |-server.js |-index.html
在http目录下运行node server.js后,此时的网站的根目录应该就是http/目录吧?
fs
模块的根目录是系统根目录…
嗯!在改了if判断后,通过输出readFile的err信息,终于发现这个问题了。原来实际是请求根目录下的index.html,而非http/index.html
thanks a lot!
我是在win下安装node的,安装路径是F:/,安装完毕后,在F盘下生成以下路径F:/nodejs
在F:/nodejs/目录下,我有两个node的程序,分别是 F:/nodejs/project/fileserver/ |-resource/style.css |-index.js |-server.js |-route.js |-requestHandler.js F:/nodejs/project/http/ |-server.js |-index.html
问题就在这里,刚才在http/下运行node server.js,无法请求localhost/index.html,而在fileserver/下运行node index.js,可以正常读取localhost/resource/style.css
这样的话,估计跟网站的根目录,是跟fs模块所在目录没关吧?网站的根目录,不应该是根据node js 入口文件所在目录而定的吗?
根据你的描述和代码,问题出在两个地方:
-
你使用了
consloe.log
而不是console.log
,这会导致代码运行时抛出错误,使得fs.exists
的回调函数不会被执行。 -
你在
fs.exists
回调中的条件判断写反了。fs.exists
确实是在检查文件或目录是否存在,如果存在则回调函数中的exists
参数为true
,反之为false
。你的原始代码里if (!exists)
是正确的逻辑,但由于上述错误,导致代码没有正常工作。
正确的代码示例
以下是修复后的代码示例:
const http = require('http');
const url = require('url');
const fs = require('fs');
http.createServer((req, res) => {
const reqPath = url.parse(req.url).pathname;
fs.exists(reqPath, (exists) => {
if (!exists) {
console.log(reqPath + ' does not exist.'); // 修改了拼写错误
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
// do something
console.log(reqPath + ' exists.');
// 读取文件内容或其他操作
}
});
}).listen(8888, () => {
console.log('Server running at http://localhost:8888/');
});
解释
- 修复拼写错误:将
consloe.log
改为console.log
。 - 正确使用
fs.exists
:如果你的文件路径正确并且文件确实存在,exists
会是true
,否则是false
。所以你的原始逻辑(if (!exists)
)是正确的。 - 处理请求:如果文件不存在,设置响应状态码为
404
并结束响应。如果文件存在,则可以根据需要进行进一步的操作。
通过这些修改,你应该可以正确地检测文件的存在性并作出相应的处理。