【发布时间】:2018-07-21 20:42:48
【问题描述】:
我使用此代码来指定文件是否存在:
if(fs.exists('../../upload/images/book/2.jpg')){
console.log("yes the file exist");
} else{
console.log("there is no file");
}
当文件在那里时,它总是说它不存在(没有文件)
我也使用fs.stat(...),但它再次给出了结果(没有文件)
文件夹结构:
root_app
--------|.tmp
--------|api
------------|controllers
------------------------|BookContoller.js (My code run from here)
--------|...(And the rest of the folders)
--------|upload
---------------|images
----------------------|book
---------------------------|2.jpg
谢谢
【问题讨论】:
-
您从哪里调用此代码,您能否通过编辑您的问题向我们展示,以包含应用程序结构和文件位置以及相对于上传文件夹的代码?
-
我建议看看这个
console.log(path.resolve('../../upload/images/book/2.jpg')),看看它到底是什么绝对路径。注意:您必须先执行const path = require('path')才能加载path模块。 -
还取决于您使用的节点版本,
fs.exists()已弃用 -
@chridam 我编辑了我的问题,我的节点版本是 9.4.0,sails.js 是 v 0.12.14
-
..内容将与当前目录相关,这取决于您如何启动 node.js 应用程序。如果您希望某些内容与模块目录相关,则应使用__dirname作为路径的基本部分。