Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani committed Dec 10, 2011
0 parents commit faad7a3
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules
*.sock
test.js
Empty file added .monitor
Empty file.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
support
test
examples
*.sock
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

0.0.1 / 2011-12-10
==================

* Initial commit

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

SRC = lib/rest-connect.js

test-server:
@node test/server
51 changes: 51 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

# Rext-connect

A simpler rext's connect router middleware

![image alt](link)

## How to use

var connect = require('connect')
, rext_connect = require('rext-connect');

app.use(connect.bodyParser());
app.use(connect.router(rext_connect.routes({"prefix":'myapp',"rext":rext})));


## Running node tests

Install dependencies:

$ npm install -d

Run them!

$ make server-test


## License

(The MIT License)

Copyright (c) 2011 Grapily <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

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 OR COPYRIGHT HOLDERS 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.
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DOCS

## sec one

### sec one.one

## sec two
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require('./lib/rext-connect');
176 changes: 176 additions & 0 deletions lib/rext-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*!
* NAME
* Copyright (c) 2011 Grapily <[email protected]>
* MIT Licensed
*/

var rext_connect = module.exports = {};

/**
* Library version
*/
rext_connect.version = '0.0.1';

function echo(options, callback) {
callback(null, JSON.stringify(options));
};

/**
* Initialize the module.
*
* @param {Object} options
* @param {String} [options.prefix="rext"] set the prefix of routes
* @param {Object} [options.rext] set the functions attached to routes
* @param {function} [options.rext.list]
* @param {function} [options.rext.create]
* @param {function} [options.rext.destroy]
* @param {function} [options.rext.retrieve]
* @param {function} [options.rext.update]
* @api public
*/

rext_connect.routes = function (options) {
var options = options || {}
, prefix = options.prefix || 'rext'
, version = 1
, pre = '/' + prefix + '/' + version
, rext = options.rext || {}
, list = rext.list || echo
, create = rext.create || echo
, destroy = rext.destroy || echo
, retrieve = rext.retrieve || echo
, update = rext.update || echo
;

return function (app) {

/**
* Define "list" route.
* Call the list function and pass a options object to it
* @params {Object} req
* @params {Object} req.params
* @params {String} [req.params.module] the module name
*/
app.get(pre + '/resources/:module?',
function (req, res, next) {
var options = {};

options['name']= req.params.module;

list(options, function (err, reply) {
if (err) res.end(err);

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain' }
);

res.end(reply);
})
}
);

/**
* Define "retrieve" route.
*
*/
app.get(pre + '/resources/:module/:version',
function (req, res, next) {
var options = {};

options['name'] = req.params.module;
options['version'] = req.params.version;

retrieve(options, function (err, reply) {
if (err) res.send(err);

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain' }
);

res.end(reply);
});
}
);

/**
* Define "destroy" route.
*
*/
app.get(pre + '/resources/:module/:version/destroy',
function (req, res, next) {
var options = {};

options['name'] = req.params.module;
options['version'] = req.params.version;

destroy(options, function (err, reply) {
if (err) res.send(err);

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain' }
);

res.end(reply);
});
}
);

/**
* Define "create" route.
*
*/
app.post(pre + '/resources/:module/:version/create',
function (req, res, next) {
var options = {};

options['name'] = req.params.module;
options['version'] = req.params.version;
options['data'] = req.body.data;

create(options, function (err, reply) {
if (err) res.send(err);

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
}
);

res.end(reply);

});
}
);

/**
* Define "update" route.
*
*/
app.post(pre + '/resources/:module/:version/update',
function (req, res, next) {
var options = {};

options['name'] = req.params.module;
options['version'] = req.params.version;
options['data'] = req.body.data;

update(options, function (err, reply) {
if (err) res.send(err);

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain' }
);

res.end(reply);
});
}
);

}
}


13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "rext-connect"
, "version": "0.0.1"
, "description": "Add rext route to connect"
, "keywords": "Grapily <dev.grapily.com>"
, "author": [""]
, "dependencies": {}
, "devDependencies": {
"connect": "1.8.2"
}
, "main": "./index.js"
, "engines": { "node": ">= 0.4.0 < 0.7.0" }
}
65 changes: 65 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<html>
<head><title>Test by hand</title></head>
<body id="index" onload="">
<div>
<h1>Default</h1>
<p>app.use(connect.router(rext_connect.routes()))</p>
<ul>
<li><a href="/rext/1/resources">List modules</a></li>
<li>
<a href="/rext/1/resources/module">List module versions</a>
</li>
<li>
<a href="/rext/1/resources/module/1.0.1">Get version 1.0.1 of module</a>
</li>
<li>
<a href="/rext/1/resources/module/1.0.1/destroy">Destroy version x.y.z of module</a>
</li>
<li>
<form action="/rext/1/resources/module/1.0.1/create" method="post">
Create version 1.0.1 of module
<input type="text" name="data" value="testo di prova"/>
<input type="submit" value="Try it" id="create"/>
</form>
</li>
<li>
<form action="/rext/1/resources/module/1.0.1/update" method="post">
Update version 1.0.1 of module
<input type="text" name="data" value="new testo di prova"/>
<input type="submit" value="Try it"/>
</form>
</li>
</ul>
</div>
<div>
<h1>Myapp</h1>
<p>app.use(connect.router(rext_connect.routes({"prefix":prefix,"rext":rext})))</p>
<ul>
<li><a href="/myapp/1/resources">List modules</a></li>
<li>
<a href="/myapp/1/resources/module">List module versions</a>
</li>
<li>
<a href="/myapp/1/resources/module/1.0.1">Get version 1.0.1 of module</a>
</li>
<li>
<a href="/myapp/1/resources/module/1.0.1/destroy">Destroy version x.y.z of module</a>
</li>
<li>
<form action="/myapp/1/resources/module/1.0.1/create" method="post">
Create version 1.0.1 of module
<input type="text" name="data" value="testo di prova"/>
<input type="submit" value="Try it" id="create"/>
</form>
</li>
<li>
<form action="/myapp/1/resources/module/1.0.1/update" method="post">
Update version 1.0.1 of module
<input type="text" name="data" value="new testo di prova"/>
<input type="submit" value="Try it"/>
</form>
</li>
</ul>
</div>
</body>
</html>
39 changes: 39 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var connect = require('../node_modules/connect/')
, rext_connect = require('../lib/rext-connect')
, app = connect()
, port = 3000
, prefix = "myapp"
;


var rext = {};
rext.list = function (options, callback) {
callback(null, "myapp LIST "+JSON.stringify(options));
};

rext.create = function (options, callback) {
callback(null, "myapp CREATE "+JSON.stringify(options));
};

rext.update = function (options, callback) {
callback(null, "myapp UPDATE "+JSON.stringify(options));
};

rext.destroy = function (options, callback) {
callback(null, "myapp DESTROY "+JSON.stringify(options));
};

rext.retrieve = function (options, callback) {
callback(null, "myapp RETRIEVE "+JSON.stringify(options));
};

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+'/');




0 comments on commit faad7a3

Please sign in to comment.