Permalink
Browse files

initial

  • Loading branch information...
1 parent 7d4e525 commit 4f977f8936f0f38b00dfc9a9a13191e42a4d07a5 @omphalos committed Jul 29, 2012
Showing with 162 additions and 0 deletions.
  1. +138 −0 file-crud-server.js
  2. +24 −0 unlicense
View
@@ -0,0 +1,138 @@
+var http = require("http");
+var fs = require('fs');
+
+var path = "c:/tmp/fs/";
+var port = 85;
+
+process.on('uncaughtException', function(err) { console.error('uncaught: ' + err); });
+
+var cleanUrl = function(url) {
+ return url.replacE(/../g, '');
+};
+
+http.createServer(function (req, res) {
+ var writeError = function (err, code) {
+ console.log('writeError-->');
+ console.log('err=' + err);
+ console.log('code=' + code);
+ code = code || 500;
+ console.log('code1=' + code);
+ console.log('Error ' + code + ': ' + err);
+ try {
+ res.statusCode = code;
+ res.setHeader('Content-Type', 'application/json');
+ res.end(JSON.stringify(err));
+ } catch(resErr) {
+ console.log('failed to write error to response: ' + resErr);
+ }
+ console.log('writeError<--');
+ };
+
+ var parsedUrl = cleanUrl(require('url').parse(req.url));
+ var query = query ? {} : require('querystring').parse(parsedUrl.query);
+ console.dir(parsedUrl);
+ var url = parsedUrl.pathname;
+ if(url.lastIndexOf('/') === url.length - 1) { url = url.slice(0, url.length - 1); }
+ if(url[0] === '/') { url = url.slice(1, url.length); }
+ console.log(req.method + ' ' + req.url);
+ console.log('url=' + url);
+ var relativePath = path + url;
+ console.log('relativePath=' + relativePath);
+ try {
+ switch(req.method) {
+ case 'GET':
+ if(url === 'favicon.ico') {
+ res.end();
+ } else {
+ fs.stat(relativePath, function(err, stats) {
+ if(err) { writeError(err); }
+ else {
+ if(stats.isDirectory()) {
+ console.log('reading directory ' + relativePath);
+ fs.readdir(relativePath, function(err, files) {
+ if(err) { writeError(err); }
+ else {
+ res.setHeader('Content-Type', 'application/json');
+ res.end(JSON.stringify(files));
+ }
+ });
+ } else {
+ 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);
+ }
+ });
+ }
+ }
+ });
+ }
+ return;
+ case 'PUT':
+ console.log('writing ' + relativePath);
+ var stream = fs.createWriteStream(relativePath);
+ stream.ok = true;
+ req.pipe(stream); // TODO: limit data length
+ req.on('end', function() {
+ if(stream.ok) {
+ res.end();
+ }
+ });
+ stream.on('error', function(err) {
+ stream.ok = false;
+ writeError(err);
+ });
+ return;
+ case 'POST':
+ if(query.rename) {
+ query.rename = cleanUrl(query.rename);
+ console.log('renaming ' + relativePath + ' to ' + path + query.rename);
+ fs.rename(relativePath, path + query.rename, function(err) {
+ if(err) { writeError(err); }
+ else {
+ res.end();
+ }
+ });
+ } else if(query.create == 'directory') {
+ console.log('creating directory ' + relativePath);
+ fs.mkdir(relativePath, 0777, function(err) {
+ if(err) { writeError(err); }
+ else {
+ res.end();
+ }
+ });
+ } else {
+ writeError('valid queries are ' + url + '?rename or ' + url + '?create=directory');
+ }
+ return;
+ case 'DELETE':
+ fs.stat(relativePath, function(err, stats) {
+ if(err) { writeError(err); }
+ else {
+ if(stats.isDirectory()) {
+ console.log('deleting directory ' + relativePath);
+ fs.rmdir(relativePath, function(err) {
+ if(err) { writeError(err); }
+ else { res.end(); }
+ });
+ } else {
+ console.log('deleting file ' + relativePath);
+ fs.unlink(relativePath, function(err) {
+ if(err) { writeError(err); }
+ else { res.end(); }
+ });
+ }
+ }
+ });
+ return;
+ default:
+ writeError('Method ' + method + ' not allowed', 405);
+ return;
+ }
+ } catch(err) {
+ writeError('unhandled error: ' + err);
+ }
+}).listen(port);
View
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>

0 comments on commit 4f977f8

Please sign in to comment.