D
D
dhat2016-10-05 04:23:10
JavaScript
dhat, 2016-10-05 04:23:10

Why doesn't the fs module create a folder?

The simplest script. Writes in the console that it cannot create a folder. When I create a folder manually - everything works.

var express = require('express')
var path = require('path')
var request = require('request')
var cheerio = require('cheerio')
var fs = require('fs')
var app = express()
var port = 8000

var destination = fs.createWriteStream('./downloads/google.html')
var url = 'http://google.com'
request(url).pipe(destination)

app.listen(port)
console.log('Server listening on: ' + port);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2016-10-05
@LiguidCool

Do you mean downloads is not created automatically? As far as I remember, this is normal behavior, it must be checked for presence and created by handles.

V
VoidVolker, 2016-10-05
@VoidVolker

Because createWrite stream is just creating a stream for writing and nothing more. And similarly with all other functions in the fs module - if this is not a directory creation, then the function will not create a directory. Therefore, before writing a file, you must make sure that all directories for this file are available. There are a lot of extensions to the fs module, for example: https://github.com/jprichardson/node-fs-extra - there are functions for automatically creating directories.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question