Skip to content

Commit d191897

Browse files
committed
Added a 2nd example of creating a custom method for the server
1 parent 5efdead commit d191897

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

examples/v2/customMethod2/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Custom method
2+
3+
This is an example to show how to make a custom method.
4+
5+
This example focus on the `TRACE` method, to send to a remote user all modified/created resources since the last request.
6+
7+
The `ts` file and the `js` file are the same thing. The `js` file displays the example in JavaScript while the `ts` file displays the example in TypeScript.
8+
9+
Note : The methods which can be added are limited to [the supported methods](https://nodejs.org/api/http.html#http_http_methods) in the `http` module.

examples/v2/customMethod2/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const webdav = require('webdav-server').v2;
2+
3+
// Server instantiation
4+
const server = new webdav.WebDAVServer();
5+
6+
let folderNotify = { };
7+
server.method('TRACE', {
8+
unchunked(ctx, data, cb)
9+
{
10+
if(!ctx.user)
11+
{
12+
ctx.setCode(webdav.HTTPCodes.Unauthorized);
13+
return cb();
14+
}
15+
16+
const path = ctx.requested.path.toString(true);
17+
const name = ctx.user.username;
18+
let lastCheck = 0;
19+
if(!folderNotify[name])
20+
folderNotify[name] = {};
21+
if(folderNotify[name][path])
22+
lastCheck = folderNotify[name][path];
23+
24+
const list = [];
25+
26+
ctx.getResource((e, r) => {
27+
r.readDir((e, files) => {
28+
let nb = files.length + 1;
29+
const go = () => {
30+
if(--nb === 0)
31+
{
32+
ctx.setCode(webdav.HTTPCodes.OK);
33+
ctx.response.write(JSON.stringify(list));
34+
folderNotify[name][path] = Date.now();
35+
cb();
36+
}
37+
}
38+
go();
39+
40+
files.forEach((file) => {
41+
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => {
42+
if(date >= lastCheck)
43+
list.push({
44+
path: path + file,
45+
name: file
46+
});
47+
48+
go();
49+
})
50+
})
51+
})
52+
})
53+
}
54+
});
55+
56+
server.start((s) => console.log('Ready on port', s.address().port));

examples/v2/customMethod2/index.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { v2 as webdav } from 'webdav-server'
2+
import * as request from 'request'
3+
4+
// Server instantiation
5+
const server = new webdav.WebDAVServer();
6+
7+
let folderNotify = { };
8+
server.method('TRACE', {
9+
unchunked(ctx : webdav.HTTPRequestContext, data : Buffer, cb : () => void)
10+
{
11+
if(!ctx.user)
12+
{
13+
ctx.setCode(webdav.HTTPCodes.Unauthorized);
14+
return cb();
15+
}
16+
17+
const path = ctx.requested.path.toString(true);
18+
const name = ctx.user.username;
19+
let lastCheck = 0;
20+
if(!folderNotify[name])
21+
folderNotify[name] = {};
22+
if(folderNotify[name][path])
23+
lastCheck = folderNotify[name][path];
24+
25+
const list = [];
26+
27+
ctx.getResource((e, r) => {
28+
r.readDir((e, files) => {
29+
let nb = files.length + 1;
30+
const go = () => {
31+
if(--nb === 0)
32+
{
33+
ctx.setCode(webdav.HTTPCodes.OK);
34+
ctx.response.write(JSON.stringify(list));
35+
folderNotify[name][path] = Date.now();
36+
cb();
37+
}
38+
}
39+
go();
40+
41+
files.forEach((file) => {
42+
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => {
43+
if(date >= lastCheck)
44+
list.push({
45+
path: path + file,
46+
name: file
47+
});
48+
49+
go();
50+
})
51+
})
52+
})
53+
})
54+
}
55+
});
56+
57+
server.start((s) => console.log('Ready on port', s.address().port));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "custom-method",
3+
"version": "1.0.0",
4+
"description": "Example to show how to make a custom method.",
5+
"main": "index.js",
6+
"author": "Adrien Castex <[email protected]>",
7+
"license": "Unlicense",
8+
"dependencies": {
9+
"webdav-server": "^2.0.0"
10+
}
11+
}

0 commit comments

Comments
 (0)