Skip to content

Quick Start Guide [v2]

Adrien Castex edited this page Jan 22, 2018 · 4 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 { v2 as webdav } from 'webdav-server'
// Javascript
const webdav = require('webdav-server').v2;

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

server.afterRequest((arg, next) => {
    console.log('>>', arg.request.method, arg.fullUri(), '>', 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 { v2 as webdav } from 'webdav-server'
// Javascript
const webdav = require('webdav-server').v2;

const server = new webdav.WebDAVServer();

server.rootFileSystem().addSubTree(server.createExternalContext(), {
    'folder1': {                                // /folder1
        'file1.txt': webdav.ResourceType.File,  // /folder1/file1.txt
        'file2.txt': webdav.ResourceType.File   // /folder1/file2.txt
    },
    'file0.txt': webdav.ResourceType.File       // /file0.txt
})

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

With auto-saving/loading :

// TypeScript
import { v2 as webdav } from 'webdav-server'
// Javascript
const webdav = require('webdav-server').v2;

const server = new webdav.WebDAVServer({
    autoSave: { // Will automatically save the changes in the 'data.json' file
        treeFilePath: 'data.json'
    }
});

// Try to load the 'data.json' file
server.autoLoad((e) => {
    if(e)
    { // Couldn't load the 'data.json' (file is not accessible or it has invalid content)
        server.rootFileSystem().addSubTree(server.createExternalContext(), {
            'folder1': {                                // /folder1
                'file1.txt': webdav.ResourceType.File,  // /folder1/file1.txt
                'file2.txt': webdav.ResourceType.File   // /folder1/file2.txt
            },
            'file0.txt': webdav.ResourceType.File       // /file0.txt
        })
    }
    
    server.start(() => console.log('READY'));
})

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

Clone this wiki locally