-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathwebdav-proxy-example.js
More file actions
83 lines (69 loc) · 2.81 KB
/
webdav-proxy-example.js
File metadata and controls
83 lines (69 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* WebDAV 代理服务器使用示例
*/
import { WebDAVProxy } from './webdav-proxy.js';
async function main() {
// 创建代理服务器实例
const proxy = new WebDAVProxy({
port: 3000,
host: 'localhost',
cacheTimeout: 5 * 60 * 1000 // 5分钟缓存
});
try {
// 启动服务器
await proxy.start();
console.log('\n=== WebDAV 代理服务器已启动 ===');
console.log('现在你可以通过以下方式访问 WebDAV 文件:');
console.log('');
// 示例:获取文件直链
console.log('1. 获取文件直链:');
console.log(' GET http://localhost:3000/file?path=/root/webdav-example.js');
console.log(' - 支持 Range 请求,适合视频流媒体播放');
console.log(' - 自动设置正确的 Content-Type');
console.log(' - 包含缓存控制头');
console.log('');
// 示例:获取文件信息
console.log('2. 获取文件信息:');
console.log(' GET http://localhost:3000/info?path=/root/webdav-example.js');
console.log(' 返回:文件大小、修改时间、内容类型等');
console.log('');
// 示例:列出目录内容
console.log('3. 列出目录内容:');
console.log(' GET http://localhost:3000/list?path=/root');
console.log(' 返回:目录下所有文件和子目录');
console.log('');
// 示例:配置 WebDAV 连接
console.log('4. 配置 WebDAV 连接:');
console.log(' POST http://localhost:3000/config');
console.log(' Body: {');
console.log(' "baseURL": "https://your-webdav-server.com",');
console.log(' "username": "your-username",');
console.log(' "password": "your-password"');
console.log(' }');
console.log('');
console.log('注意:');
console.log('- 如果没有通过 /config 配置,服务器会尝试从 json/webdav.json 读取默认配置');
console.log('- 所有文件访问都会自动缓存 5 分钟以提高性能');
console.log('- 支持 CORS,可以从浏览器直接访问');
console.log('- 使用 Fastify 框架,性能更优');
console.log('');
// 等待用户中断
console.log('按 Ctrl+C 停止服务器...');
// 保持进程运行
await new Promise(() => {});
} catch (error) {
console.error('启动服务器失败:', error);
process.exit(1);
}
}
// 优雅关闭处理
let proxy;
process.on('SIGINT', async () => {
console.log('\n正在关闭服务器...');
if (proxy) {
await proxy.stop();
}
process.exit(0);
});
// 运行示例
main().catch(console.error);