diff --git a/.gitignore b/.gitignore index f2f741b..7468097 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store node_modules +test/repo *.sock -test.js +*~ diff --git a/.monitor b/.monitor new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile index 2275937..240ef98 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ - +REPODIR = test/repo SRC = lib/rest-connect.js -test-server: +$(REPODIR): + mkdir -p $(REPODIR) + +test-server: $(REPODIR) @node test/server diff --git a/lib/rext-connect.js b/lib/rext-connect.js index b971936..1bc7844 100644 --- a/lib/rext-connect.js +++ b/lib/rext-connect.js @@ -11,6 +11,20 @@ var rext_connect = module.exports = {}; */ rext_connect.version = '0.0.2'; + +/** + * Send back an error message to the client. + * @param {Response} res http.ServerResponse Object + * @param {String} message Text to send + */ +function sendError(res, message) { + res.writeHead(400, { + 'Content-Length': message.length, + 'Content-Type': 'text/plain' + }) + res.end(message); +} + /** * Initialize the module. * @@ -31,45 +45,43 @@ rext_connect.routes = function (options) { , pre = '/' + prefix + '/' + version , rext = options.rext || {} , paths = { - destroy: pre + '/resources/:module/:version/destroy' - , list: pre + '/resources/:module?' - , retrieve: pre + '/resources/:module/:version' - , create: pre + '/resources/:module/:version/create' - , update: pre + '/resources/:module/:version/update' + destroy: pre + '/resources/:document/:version/destroy' + , list: pre + '/resources/:document?' + , retrieve: pre + '/resources/:document/:version' + , create: pre + '/resources/:document/:version/create' + , update: pre + '/resources/:document/:version/update' } ; return function (app) { app.get(paths.list, function (req, res, next) { - var options = { - name: req.params.module - }; - - rext.list(options, function (err, reply) { + var name = req.params.document; + + rext.list(name, function (err, reply) { if (err) { - res.send(err); + sendError(res, err.message); return; } - + var data = JSON.stringify(reply); res.writeHead(200, { - 'Content-Length': reply.length, - 'Content-Type': 'text/plain' + 'Content-Length': data.length, + 'Content-Type': 'application/json' }); - - res.end(reply); + + res.end(data); }); }); app.get(paths.retrieve, function (req, res, next) { var options = { - name: req.params.module, + name: req.params.document, version: req.params.version }; rext.retrieve(options, function (err, reply) { if (err) { - res.send(err); + res.end(err.message); return; }; @@ -77,74 +89,63 @@ rext_connect.routes = function (options) { 'Content-Length': reply.length, 'Content-Type': 'text/plain' }); - - res.end(reply); + + //put the line below in the server + //callback(null, fs.createReadStream(filePath)); + //reply.pipe(res); + res.end(reply.toString('utf8')); }); }); app.get(paths.destroy, function (req, res, next) { var options = { - name: req.params.module, + name: req.params.document, version: req.params.version }; rext.destroy(options, function (err, reply) { if (err) { - res.send(err); + sendError(res, err.message); return; } - - res.writeHead(200, { - 'Content-Length': reply.length, - 'Content-Type': 'text/plain' - }); - - res.end(reply); + + res.end(); }); }); app.post(paths.create, function (req, res, next) { var options = { - name: req.params.module, + name: req.params.document, version: req.params.version, data: req.body.data }; rext.create(options, function (err, reply) { if (err) { - res.send(err); + sendError(res, err.message); return; } - res.writeHead(200, { - 'Content-Length': reply.length, - 'Content-Type': 'text/plain' - }); - - res.end(reply); + res.end(); }); }); app.post(paths.update, function (req, res, next) { + var options = { - name: req.params.module, + name: req.params.document, version: req.params.version, data: req.body.data }; rext.update(options, function (err, reply) { if (err) { - res.send(err); + sendError(res, err.message); return; } - res.writeHead(200, { - 'Content-Length': reply.length, - 'Content-Type': 'text/plain' - }); - - res.end(reply); + res.end(); }); }); }; diff --git a/package.json b/package.json index 3121d4c..5487efb 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ , "author": [""] , "dependencies": {} , "devDependencies": { - "connect": "1.8.2" + "connect": "1.8.2" + , "rext": ">= 0.0.5" } , "main": "./index.js" , "engines": { "node": ">= 0.4.0 < 0.7.0" } diff --git a/test/index.html b/test/index.html index 78ef593..e88385a 100644 --- a/test/index.html +++ b/test/index.html @@ -3,79 +3,35 @@
app.use(connect.router(rext_connect.routes({"rext":echo})))
+app.use(connect.router(rext_connect.routes({"rext":rext})))
- - List modules
- should return {}
+ - List document
- - List module versions
- should return {"name": "module"}
+ - List document versions
- - Get version 1.0.1 of module
- should return {"name": "module", "version": "1.0.1"}
+ - Get version 1.0.1 of document
- - Destroy version x.y.z of module
- should return {"name": "module", "version": "1.0.1"}
+ - Destroy version 1.0.1 of document
-
- should return {"name": "module", "version": "1.0.1", "data": "testo di prova"}-
- should return {"name": "module", "version": "1.0.1", "data": "new testo di prova"}app.use(connect.router(rext_connect.routes({"prefix":"myapp","rext":rext})))
-
- - List modules
- should return myappLIST {}
-
- - List module versions
- should return myappLIST {"name": "module"}
-
- - Get version 1.0.1 of module
- should return myappRETRIEVE {"name": "module", "version": "1.0.1"}
-
- - Destroy version x.y.z of module - should return myappDESTROY {"name": "module", "version": "1.0.1"} -
--
- should return myappCREATE {"name": "module", "version": "1.0.1", "data": "testo di prova"} - --
- should return myappUPDATE {"name": "module", "version": "1.0.1", "data": "new testo di prova"} - -