Skip to content

Quick Start Guide

Adrien Castex edited this page Jun 13, 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

Fast/test version :

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

const server = new webdav.WebDAVServer({
    port: 1900
});

server.afterRequest((arg, next) => {
    console.log('>>', arg.request.method, arg.uri, '>', arg.response.statusCode, arg.response.statusMessage);
    next();
})

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

Basic version :

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

// 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');
// Open the file in write mode
file.write(true, (e, wstream) => {
    if(e) throw e;

    // Set the content of the virtual file and close it (end)
    wstream.end('The content of the virtual file.', (e) => {
        if(e) throw e;

        // 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'
import * as zlib from 'zlib'
import * as fs from 'fs'
// Javascript
const webdav = require('webdav-server'),
      zlib = require('zlib')
      fs = require('fs');

const persistenceFilePath = './persistence.data';

// Create a WebDAV server with options
const server = new webdav.WebDAVServer({
    port: 1900,
    autoSave: {
        treeFilePath: persistenceFilePath,
        tempTreeFilePath: persistenceFilePath + '.tmp'
    }
});

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

    server.load(JSON.parse(zlib.unzipSync(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.write(true, (e, wstream) => {
        if(e) throw e;

        wstream.end('The content of the virtual file.', (e) => {
            if(e) throw e;

            // 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!');
    })
}

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

Clone this wiki locally