WebDAV Server for Node.js
webdav-server is a robust and highly configurable WebDAV server implementation for Node.js, currently stable at version 2.6.2. It allows developers to create a WebDAV endpoint capable of serving various types of resources, including physical files and folders from a hard drive, in-memory virtual resources, dynamically processed/computed content, and entirely custom resource types, all within a single server instance. The project maintains an active release cadence with frequent updates, as seen with multiple patch and minor releases since its major version 2.0.0. Key differentiators include its flexible architecture that supports custom user managers, resource types, HTTP methods, and server state persistence, making it suitable for diverse applications from human-readable content management via WebDAV clients to inter-program information sharing with temporary file systems. It adheres strictly to RFC4918.
Common errors
-
Error: listen EADDRINUSE: address already in use :::8080
cause The network port configured for the WebDAV server (e.g., 8080) is currently occupied by another application or a previous server instance that was not properly shut down.fixChange the `port` option when creating your `WebDAVServer` instance to an available port (e.g., 8081), or identify and terminate the process currently using the desired port. -
TypeError: Path must be a string. Received undefined (at ... PhysicalFileSystem.constructor)
cause The `PhysicalFileSystem` was initialized with an `undefined` or non-string path, typically due to an unhandled variable or incorrect path resolution logic.fixEnsure that the root path provided to `new PhysicalFileSystem()` is always a valid, absolute string. Use `path.resolve(__dirname, 'your-webdav-root')` to guarantee an absolute path. -
HTTP/1.1 405 Method Not Allowed (from client side)
cause A WebDAV client is attempting an operation (e.g., PROPFIND, PUT, DELETE) that is not permitted for the requested resource or due to the server's access control configuration. This often indicates a missing privilege, incorrect resource type, or misconfigured method support.fixVerify that your `UserManager` and `PrivilegeManager` (if implemented) grant the necessary permissions. Ensure the target resource correctly implements the desired WebDAV methods. Check if the client is expecting a file when it's a folder, or vice-versa.
Warnings
- breaking Version 2.0.0 introduced significant internal changes and API modifications. Specifically, the `RequestContext` class properties and how `PROPPATCH` attributes are handled were updated, potentially breaking existing implementations that relied on version 1.x APIs.
- gotcha Deploying a `WebDAVServer` with `PhysicalFileSystem` without implementing custom authentication, authorization, or robust path validation can expose your server to unauthorized access and potential data breaches.
- gotcha The `PhysicalFileSystem` constructor takes a root path. Using relative paths (e.g., `'.'`) can lead to unexpected behavior depending on the process's current working directory. Misconfigured paths can also lead to unintended directory exposure.
- gotcha The default configuration of `webdav-server` does not include any authentication or authorization mechanisms. Without these, anyone can access and modify resources on your WebDAV server.
- deprecated The `strictMode` option (introduced in v1.6.0) can affect client compatibility. While offering flexibility, deviating from strict RFC4918 compliance might cause issues with some WebDAV clients.
Install
-
npm install webdav-server -
yarn add webdav-server -
pnpm add webdav-server
Imports
- WebDAVServer
const WebDAVServer = require('webdav-server')import { WebDAVServer } from 'webdav-server' - PhysicalFileSystem
import PhysicalFileSystem from 'webdav-server/lib/manager/PhysicalFileSystem'
import { PhysicalFileSystem } from 'webdav-server' - VirtualFileSystem
import * as VFS from 'webdav-server/virtual'
import { VirtualFileSystem } from 'webdav-server' - WebDAVServerOptions
import { WebDAVServerOptions } from 'webdav-server'
Quickstart
import { WebDAVServer, PhysicalFileSystem } from 'webdav-server';
import * as http from 'http';
import * as path from 'path';
// Define the port for the WebDAV server
const port = 8080;
// Define the root directory for the physical file system
// For a production environment, use a robust path resolution method
const physicalRootPath = path.resolve(__dirname, 'webdav-root');
// Create a new WebDAV server instance
const server = new WebDAVServer({
port: port,
// debug: true, // Uncomment for detailed logging
// Set the server name and version for HTTP headers
serverName: 'My WebDAV Server',
version: '1.0.0'
});
// Configure a physical file system at the root '/physical' of the WebDAV server
// This maps the physicalRootPath to the WebDAV path.
server.setFileSystem('/physical', new PhysicalFileSystem(physicalRootPath));
// Alternatively, for a virtual file system:
// server.setFileSystem('/virtual', new VirtualFileSystem());
// Start the HTTP server to listen for WebDAV requests
server.start((s: http.Server) => {
console.log(`WebDAV server is listening on http://localhost:${port}`);
console.log(`Serving physical files from: ${physicalRootPath}`);
console.log('You can now connect with a WebDAV client (e.g., Cyberduck, Windows Network Drive).');
console.log('Example: Connect to http://localhost:8080/physical');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nStopping WebDAV server...');
server.stop(() => {
console.log('WebDAV server stopped.');
process.exit(0);
});
});