This repository was archived by the owner on Feb 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile.js
More file actions
66 lines (55 loc) · 1.45 KB
/
file.js
File metadata and controls
66 lines (55 loc) · 1.45 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
//
// deserver file server component
//
// This component provides a REST-like, memory-backed file server.
//
// Interface:
//
// PUT {url}
// 201 create a new resource
// 405 resource already exists
//
// GET {url}
// 200 retrieve existing resource
// 404 not found
//
// DELETE {url}
// 200 delete an existing resource
// 404 not found
//
// Any request with an other method yields 405.
//
exports.create = function () {
var cache = {};
var slurp = require('./util').slurp;
var end = require('./util').end;
var methods = {};
methods.GET = function (req, res) {
if (!cache.hasOwnProperty(req.url))
return end(req, res, 404);
var file = cache[req.url];
end(req, res, 200, { 'content-type': file.type }, file.content);
};
methods.PUT = function (req, res) {
if (cache.hasOwnProperty(req.url))
return end(req, res, 405, { 'allow': 'GET, DELETE' });
return slurp(req, function (content) {
cache[req.url] = {
type: req.headers['content-type'],
content: content
};
return end(req, res, 201);
});
};
methods.DELETE = function (req, res) {
if (!cache.hasOwnProperty(req.url))
return end(req, res, 404);
delete cache[req.url];
return end(req, res, 200);
};
return function (req, res) {
if (!methods.hasOwnProperty(req.method))
return end(req, res, 405, { 'allow': Object.keys(methods) });
return methods[req.method](req, res);
};
};