Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support HEAD & read only from command line

  • Loading branch information...
commit 37acc8ce128e089627be9a7a89d71f053f050d1a 1 parent 89c3343
@omphalos authored
View
3  README.md
@@ -12,8 +12,9 @@ This starts a file server using the specified command-line options.
-f file system path to expose over http
-p port to listen on (example, 80)
- -v virtual path to host the file server on
-q suppress this message
+ -r read only
+ -v virtual path to host the file server on
Example:
View
5 bin/crud-file-server
@@ -13,8 +13,9 @@ if(!argv.q) {
console.log('');
console.log(' -f file system path to expose over http');
console.log(' -p port to listen on (example, 80)');
- console.log(' -v virtual path to host the file server on');
console.log(' -q suppress this message');
+ console.log(' -r read only');
+ console.log(' -v virtual path to host the file server on');
console.log('');
console.log('example:');
console.log('');
@@ -25,10 +26,10 @@ if(!argv.q) {
var port = argv.p || 80;
var path = argv.f || process.cwd();
var vpath = (argv.v || '').trimLeft();
+var readOnly = argv.v;
require('http').createServer(function (req, res) {
server.handleRequest(vpath, path, req, res);
}).listen(port);
-console.log('server started');
console.log('listening on :' + port + '/' + vpath + ', serving ' + path);
View
21 crud-file-server.js
@@ -58,6 +58,7 @@ exports.handleRequest = function(vpath, path, req, res, readOnly) {
writeError(req.method + ' forbidden on this resource', 403);
} else {
switch(req.method) {
+ case 'HEAD':
case 'GET': // returns file or directory contents
if(url === 'favicon.ico') {
res.end(); // if the browser requests favicon, just return an empty response
@@ -72,7 +73,10 @@ exports.handleRequest = function(vpath, path, req, res, readOnly) {
if(err) { writeError(err); }
else {
res.setHeader('Content-Type', 'application/json');
- res.end(JSON.stringify(files));
+ if(req.method != 'HEAD') {
+ res.write(JSON.stringify(files));
+ }
+ res.end();
}
});
} else {
@@ -80,12 +84,15 @@ exports.handleRequest = function(vpath, path, req, res, readOnly) {
console.log('reading file ' + relativePath);
var type = require('mime').lookup(relativePath);
res.setHeader('Content-Type', type);
- fs.readFile(relativePath, function(err, data) {
- if(err) { writeError(err); }
- else {
- res.end(data);
- }
- });
+ if(req.method == 'HEAD') { res.end(); }
+ else {
+ fs.readFile(relativePath, function(err, data) {
+ if(err) { writeError(err); }
+ else {
+ res.end(data);
+ }
+ });
+ }
}
}
});
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "crud-file-server",
- "version": "0.0.7",
+ "version": "0.0.9",
"description": "file server supporting basic create, read, update, & delete for any kind of file",
"bin": {
"crud-file-server": "./bin/crud-file-server"
Please sign in to comment.
Something went wrong with that request. Please try again.