I am working with node and express and i have a directory structure like
public
img
css
js
server.js
in server.js i have the following code for writing to a png file
fs.writeFile('testfile.png', imageData, function(err){
res.send("file successfully created");
});
this creates the file in the root i.e. with the following directory structure
public
img
css
js
server.js
testfile.png
How do I create testfile.png
inside the img
directory? simply using img/testfile.png
, /img/testfile.png
or /public/img/testfile.png
does not work-- no file is created.
Help appreciated!
I am working with node and express and i have a directory structure like
public
img
css
js
server.js
in server.js i have the following code for writing to a png file
fs.writeFile('testfile.png', imageData, function(err){
res.send("file successfully created");
});
this creates the file in the root i.e. with the following directory structure
public
img
css
js
server.js
testfile.png
How do I create testfile.png
inside the img
directory? simply using img/testfile.png
, /img/testfile.png
or /public/img/testfile.png
does not work-- no file is created.
Help appreciated!
Share Improve this question asked Feb 5, 2013 at 4:37 algorithmicCoderalgorithmicCoder 6,79521 gold badges75 silver badges118 bronze badges1 Answer
Reset to default 7You can do this
'./public/img/testfile.png'
Or, you can give relative path like this
__dirname + '/public/img/testfile.png'
Also check for the permission of the directory img
. May be the write permission is not given.