请教 fs.exists() 方法
发布于 10 年前 作者 tsregll 5760 次浏览 最后一次编辑是 8 年前

最近在看TJ大神的 node in action 实验了其中的socket实时聊天网页,启动后报404错误,也就是找不到index.htm页,不知道各位有没有遇到,自己对了一下写的路径,应该没什么错误,请教大家,读取文件的话路径应该怎样写?

15 回复
__dirname + "/yourfile.txt"

签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

这是代表根目录下的txt文件么?

no, mean is current code path.


签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

@brighthas给个路径的实例呗,作者给的inde.htm的路径是“./public/index.htm”

能给个 __dirname

@brighthas 兄弟,你这个太贵了…

@ringtail lol , many people buy, before the end of price increases.


签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

__dirname? is working directory or source file directory?

@brighthas__dirname是什么?给一个实例吧。如:"./publc" 其中"./"代表什么什么

@tsregll

This is the basic knowledge you need to educate yourself.


签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3

@brighthas Well Ok I’ll continue to read node in action ,thank you so much

是应该补补基础知识了,全局对象啊。。。

__dirname 项目路径 ./ 当前路径

module.exports = function () { console.log(‘启动的目录:’ + process.cwd()); console.log(‘工作的目录:’ + __dirname); }

回到顶部