Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  102] [ 4]  / answers: 1 / hits: 5711  / 11 Years ago, sun, december 1, 2013, 12:00:00

I have been trying to create a simple html file in the public folder of my Express.js app, but can't get the path right. Can anybody help me out?



Here is the part of my app.js where I configure my static folder:



app.use(express.static(path.join(__dirname, 'public')));


and here's the code I'm using to try to create the index.html file in the public folder:



exports.index = function(req, res){
var fs = require('fs');
fs.openSync(__dirname + /public/static_html/index.html, 'w')
};


However, node.js throw an error:




500 Error: ENOENT, no such file or directory
'C:nodejsnode_modules.binmyapproutespublicstatic_htmlindex.html'




Why is it pointing to the to the public folder inside routes?



I would like the path to be:




'C:nodejsnode_modules.binmyapppublicstatic_htmlindex.html'




Can anybody help please?


More From » node.js

 Answers
0

My guess would be that your directory structure looks like this:



app.js
public/
static_html/
routes/
index.js


And that your exports.index exists in routes/index.js (perhaps it's named differently, but I'm just guessing here).



Since __dirname is relative to the module, it will be ./routes in that file. So you need to go back one level to be able to access public/:



fs.openSync(__dirname + /../public/static_html/index.html, 'w')

[#49953] Friday, November 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinah

Total Points: 268
Total Questions: 113
Total Answers: 89

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;