在Express.js应用程序的公共文件夹中创build一个文件
我一直在尝试在我的Express.js应用程序的公用文件夹中创build一个简单的html文件,但无法获得正确的path。 有人可以帮我吗?
这里是我的app.js我configuration我的静态文件夹的一部分:
app.use(express.static(path.join(__dirname, 'public')));
这里是我用来尝试在公用文件夹中创buildindex.html文件的代码:
exports.index = function(req, res){ var fs = require('fs'); fs.openSync(__dirname + "/public/static_html/index.html", 'w') };
但是,node.js抛出一个错误:
500错误:ENOENT,没有这样的文件或目录'C:\ nodejs \ node_modules.bin \ myapp \ routes \ public \ static_html \ index.html'
为什么它指向“路线”内的公共文件夹?
我想要的path是:
'C:\的NodeJS \ node_modules.bin \ MYAPP \ PUBLIC \ static_html \ index.html在'
有人可以帮忙吗?
我的猜测是你的目录结构如下所示:
app.js public/ static_html/ routes/ index.js
而你的exports.index
存在于routes/index.js
(也许它的命名有所不同,但我只是在这里猜测)。
由于__dirname
是相对于模块,它将在该文件中./routes
。 所以你需要回到一个级别才能访问public/
:
fs.openSync(__dirname + "/../public/static_html/index.html", 'w')