Permalink
Browse files

support json requests for data

  • Loading branch information...
1 parent b04fb3a commit 3d812721a11d53d424afc0641420df23d1390baf @omphalos committed Aug 17, 2012
Showing with 25 additions and 13 deletions.
  1. +24 −12 crud-file-server.js
  2. +1 −1 package.json
View
@@ -50,7 +50,7 @@ exports.handleRequest = function(vpath, path, req, res, readOnly, logHeadRequest
if(req.method != 'HEAD') {
console.log(req.method + ' ' + req.url);
}
- var relativePath = vpath ?
+ var relativePath = vpath && url.indexOf(vpath) == 0 ?
path + url.slice(vpath.length + 1, url.length):
path + url;
@@ -65,12 +65,12 @@ exports.handleRequest = function(vpath, path, req, res, readOnly, logHeadRequest
}
fs.stat(relativePath, function(err, stats) { // determine if the resource is a file or directory
if(err) { writeError(err); }
- else {
+ else {
res.setHeader('Last-Modified', stats.mtime);
if(stats.isDirectory()) {
- res.setHeader('Content-Type', query.dir == 'json' ? 'application/json' : 'text/html');
+ res.setHeader('Content-Type', query.type == 'json' || query.dir == 'json' ? 'application/json' : 'text/html');
} else {
- var type = require('mime').lookup(relativePath);
+ var type = query.type == 'json' || query.dir == 'json' ? 'application/json' : require('mime').lookup(relativePath);
res.setHeader('Content-Type', type);
}
res.end();
@@ -137,14 +137,25 @@ exports.handleRequest = function(vpath, path, req, res, readOnly, logHeadRequest
} else {
// if it's a file, return the contents of a file with the correct content type
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(query.type == 'json' || query.dir == 'json') {
+ var type = 'application/json';
+ res.setHeader('Content-Type', type);
+ fs.readFile(relativePath, function(err, data) {
+ if(err) { writeError(err); }
+ else {
+ res.end(JSON.stringify({ data: data.toString() }));
+ }
+ });
+ } else {
+ 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);
+ }
+ });
+ }
}
}
});
@@ -170,6 +181,7 @@ exports.handleRequest = function(vpath, path, req, res, readOnly, logHeadRequest
console.log('rename: ' + relativePath);
// e.g., http://localhost/old-name.html?rename=new-name.html
query.rename = cleanUrl(query.rename);
+ // TODO: handle missing vpath here
if(vpath) {
if(query.rename.indexOf('/' + vpath + '/') == 0) {
query.rename = query.rename.slice(vpath.length + 2, query.rename.length);
View
@@ -1,6 +1,6 @@
{
"name": "crud-file-server",
- "version": "0.1.9",
+ "version": "0.2.2",
"description": "file server supporting basic create, read, update, & delete for any kind of file",
"bin": {
"crud-file-server": "./bin/crud-file-server"

0 comments on commit 3d81272

Please sign in to comment.