Skip to content

Quick Start Guide

Adrien Castex edited this page May 26, 2017 · 5 revisions

Install the module and import it into your own project

You can find the install/import steps here.

Create your first WebDAV server from the module

Basic version :

// Typescript
import * as webdav from 'webdav-server'
// Javascript
const webdav = require('webdav-server');

// Create a WebDAV server with options
const server = new webdav.WebDAVServer({
    port: 1900
});

// Create a default resource tree :

// Create a virtual file
const file = new webdav.VirtualFile('fileWithContent.txt');
// Set the content of the virtual file
file.content = 'The content of the virtual file.';

// Add the virtual resources to the root folder
// Note that you can add resources even when the
// server is running
server.addResourceTree({
    r: new webdav.VirtualFolder('myFirstFolder'),   // path : /myFirstFolder
    c: [
        {
            r: new webdav.VirtualFolder('folder1'), // path : /myFirstFolder/folder1
            c: new webdav.VirtualFile('file2.txt')  // path : /myFirstFolder/folder1/file2.txt
        },
        file                                        // path : /myFirstFolder/fileWithContent.txt
    ]
}, e => {
    if(e)
        throw e;
    
    // Created

    startTheServer(server);
});

function startTheServer(server)
{
    // Start the server
    server.start(httpServer => {
        console.log('Server started with success on the port : ' + httpServer.address().port);

        // [...]
    });
}

function stopTheServer(server)
{
    // Stop the server
    server.stop(() => {
        console.log('Server stopped with success!');
    })
}

With auto-saving :

// Typescript
import * as webdav from 'webdav-server'
// Javascript
const webdav = require('webdav-server');

// Create a WebDAV server with options
const server = new webdav.WebDAVServer({
    port: 1900
});

const persistenceFilePath = './persistence.data';


if(fs.existsSync(persistenceFilePath))
{ // Load the files from the save file 'persistence.data'

    server.load(JSON.parse(fs.readFileSync(persistenceFilePath)), [
        new webdav.PhysicalFSManager(),
        new webdav.VirtualFSManager(),
        new webdav.RootFSManager()
    ], (e) => {
        if(e)
            throw e;
        
        // Loaded

        startTheServer(server);
    });
}
else
{ // Create a default resource tree when none already exists (no save file found)

    // Create a virtual file
    const file = new webdav.VirtualFile('fileWithContent.txt');
    // Set the content of the virtual file
    file.content = 'The content of the virtual file.';

    // Add the virtual resources to the root folder
    // Note that you can add resources even when the
    // server is running
    server.addResourceTree({
        r: new webdav.VirtualFolder('myFirstFolder'),   // path : /myFirstFolder
        c: [
            {
                r: new webdav.VirtualFolder('folder1'), // path : /myFirstFolder/folder1
                c: new webdav.VirtualFile('file2.txt')  // path : /myFirstFolder/folder1/file2.txt
            },
            file                                        // path : /myFirstFolder/fileWithContent.txt
        ]
    }, e => {
        if(e)
            throw e;
        
        // Created

        startTheServer(server);
    });
}

function startTheServer(server)
{
    // Save the server after every request
    server.afterRequest((arg, next) => {
        server.save((e, data) => {
            fs.writeFile(persistenceFilePath, JSON.stringify(data, null, 4), e => {
                if(e)
                    throw e;
                
                // Saved
            })
        })
        next();
    })
    
    // Start the server
    server.start(httpServer => {
        console.log('Server started with success on the port : ' + httpServer.address().port);

        // [...]
    });
}

function stopTheServer(server)
{
    // Stop the server
    server.stop(() => {
        console.log('Server stopped with success!');
    })
}

To see more, you are invited to visit the examples page.

Clone this wiki locally