1

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!

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

does the first one work? img is under public not the current directory?
Oops. forgot to add public, edited.. did you check permissions?
ok that works...although i just realized i was not restarting the node server!..that probably has more to do with my issues :) will accept your answer though. Thanks!
How do you do this with ecmascript modules, in a way that ESLINT won't complain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.