Node fs module creates a new file

  node.js, question

Use fs module to create a file, such as public/statement/out.txt, but statement does not exist in this directory. I use it directly:

fs.createWriteStream('public/statement/out.txt')

The error message is directory without this. What should I do?

Write a function that creates a directory, such as

Varcreatefolder = function (to) {//file write
 var sep = path.sep
 var folders = path.dirname(to).split(sep);
 var p = '';
 while (folders.length) {
 Shift () plus sep;
 if (!  fs.existsSync(p)) {
 fs.mkdirSync(p);
 bracket
 bracket
 };

CreateFolder is called before fs.createWriteStream is called, such as

createFolder('/publis/statemenet/out.txt');
 fs.createWriteStream('public/statement/out.txt')