algorithmicCoder
algorithmicCoder

Reputation: 6799

Write file to a subdirectory node.js

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.pnginside 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!

Upvotes: 1

Views: 4248

Answers (1)

Salman
Salman

Reputation: 9447

You 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.

Upvotes: 7

Related Questions