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 @@

Default

-

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

-

- - Create version 1.0.1 of module + + - Create version 1.0.1 of document
- should return {"name": "module", "version": "1.0.1", "data": "testo di prova"}

-

- - Update version 1.0.1 of module + + - Update version 1.0.1 of document
- should return {"name": "module", "version": "1.0.1", "data": "new testo di prova"}

-
-

Myapp

-

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"} -

-

-

- - Create version 1.0.1 of module - - -
- should return myappCREATE {"name": "module", "version": "1.0.1", "data": "testo di prova"} -

-

-

- - Update version 1.0.1 of module - - -
- should return myappUPDATE {"name": "module", "version": "1.0.1", "data": "new testo di prova"} -

-
-
\ No newline at end of file diff --git a/test/server.js b/test/server.js index 0b8d35c..a3c2be1 100644 --- a/test/server.js +++ b/test/server.js @@ -1,40 +1,16 @@ var connect = require('../node_modules/connect/') , rext_connect = require('../lib/rext-connect') + , Rext = require('../node_modules/rext') , app = connect() , port = 3000 - , prefix = "myapp" + , prefix = "echo" ; -function echo(options, callback) { - callback(null, JSON.stringify(options)); -}; - -function rext_function(name) { - return function (options, callback) { - callback(null, prefix + name + " " + JSON.stringify(options)); - } -} - -var rext = { - list: rext_function("LIST"), - create: rext_function("CREATE"), - update: rext_function("UPDATE"), - destroy: rext_function("DESTROY"), - retrieve: rext_function("RETRIEVE"), -}; - -var echo = { - list: echo, - create: echo, - update: echo, - destroy: echo, - retrieve: echo -}; +var rext = new Rext("./test/repo/"); app.use(connect.bodyParser()); app.use(connect(connect.static(__dirname))); -app.use(connect.router(rext_connect.routes({"prefix": prefix,"rext": rext}))); -app.use(connect.router(rext_connect.routes({"rext":echo}))).listen(port); +app.use(connect.router(rext_connect.routes({"rext":rext}))).listen(port); console.log('Server running at http://127.0.0.1:' + port + '/'); diff --git a/test/server.js~ b/test/server.js~ deleted file mode 100644 index ed5f1e0..0000000 --- a/test/server.js~ +++ /dev/null @@ -1,43 +0,0 @@ -var connect = require('../node_modules/connect/') - , rext_connect = require('../lib/rext-connect') - , app = connect() - , port = 3000 - , prefix = "myapp" - ; - -function echo(options, callback) { - callback(null, JSON.stringify(options)); -}; - -function rext_function(name) { - return function (options, callback) { - callback(null, prefix + name + " " + JSON.stringify(options)); - } -} - -var rext = { - list: rext_function("LIST"), - create: rext_function("CREATE"), - update: rext_function("UPDATE"), - destroy: rext_function("DESTROY"), - retrieve: rext_function("RETRIEVE"), -}; - -var echo = { - list: echo, - create: echo, - update: echo, - destroy: echo, - retrieve: echo -}; - -app.use(connect.bodyParser()); -app.use(connect(connect.static(__dirname))); -app.use(connect.router(rext_connect.routes({"prefix": prefix,"rext": rext}))); -app.use(connect.router(rext_connect.routes())).listen(port); - -console.log('Server running at http://127.0.0.1:' + port + '/'); - - - -