Node fs wrapper for WebDAV. Perform basic filesystem tasks in a similar manner to using async fs methods like readdir and writeFile. webdav-fs uses webdav-client under the hood.
Install webdav-fs using npm:
npm install webdav-fs --save
You can use webdav-fs in authenticated or non-authenticated mode:
// Using authentication:
var wfs = require("webdav-fs")(
"http://example.com/webdav/",
"username",
"password"
);
wfs.readdir("/Work", function(err, contents) {
if (!err) {
console.log("Dir contents:", contents);
} else {
console.log("Error:", err.message);
}
});// Without using authentication:
var wfs = require("webdav-fs")("http://example.com/webdav/");
wfs.stat("/report.docx", function(err, data) {
console.log("Is file:", data.isFile());
});You can read the API documentation here, or check out the examples below.
The following methods are available on the webdav-fs module:
Create a read stream on a remote file:
wfs
.createReadStream("/dir/somefile.dat")
.pipe(fs.createWriteStream("./somefile.dat"));The options object supports overriding remote headers as well as a range (start and end as byte indexes). When specifying a range, only the start value is required (if end is not provided the rest of the file is read).
The following requests the first 300 bytes of a file:
var myPartialStream = wfs.createReadStream("/dir/somefile.dat", { start: 0, end: 299 });Create a write stream for a remote file:
fs
.createReadStream("./myFile.dat")
.pipe(wfs.createWriteStream("/data/myFile.dat"));The options object supports overriding remote headers.
Create a remote directory:
wfs.mkdir("/remote/dir", function(error) {
// handle error if truthy
});Read the contents of a remote directory:
wfs.readdir("/some/remote/path/", function(error, contents) {
// callback is an array of filenames
});mode is an optional processing mode, where:
- 'node' (the default mode) will output an array of filename strings
- 'stat' will output an array of stat objects (plus a
namefield)
Read the contents of a remote file:
wfs.readFile("/website/index.php", "utf8", function(error, data) {
// data is the contents of the file
// encoding is optional
});Move/rename a file to another location/name. This does not create new directories for nested files (moving a file into a new directory will not work).
wfs.rename("/my-document.docx", "/Documents/personal.docx", function (error) {
// handle error
});Stat a remote file:
wfs.stat("/the-internet.dat", function(error, fileStat) {
console.log(fileStat);
});A stat has the following properties:
| Property | Type | Description |
|---|---|---|
| isFile | Function | Check if the item is a file |
| isDirectory | Function | Check if the item is a directory |
| mtime | Number | Last modification timestamp |
| size | Number | Size of the item in bytes |
Delete a remote file or directory:
wfs.unlink("/remote/path", function(error) {
// handle error if truthy
});Write data to a remote file:
wfs.writeFile("/Temp/im-here.txt", "This is a saved file! REALLY!!", function(err) {
console.error(err.message);
});writeFile supports writing binary files as well:
fs.readFile(sourceFile, "binary", function(err, data) {
wfs.writeFile(destFile, data, "binary", function(err) {
// handle error
});
});writeFile supports just a couple of encodings:
- utf8
- binary
When writing binary files, data must either be a binary string from a read file in Node (then passed to new Buffer(data, "binary")) or a Buffer.
Under the hood, webdav-client uses node-fetch to perform requests. This can be overridden by running the following:
// For example, use the `fetch` method in the browser:
const createWebDAVfs = require("webdav-fs");
createWebDAVfs.setFetchMethod(window.fetch);Refer to webdav-client's documentation for more information.