Skip to content

Commit

Permalink
[refactor] Replace journey with director.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed May 16, 2013
1 parent 0b1fe1c commit 61ae523
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# nodejs-intro

My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with journey, cradle, winston, optimist, and http-console.
My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with director, cradle, winston, optimist, and http-console.

#### Author: [Charlie Robbins](http://twitter.com/indexzero)

[0]: http://github.com/cloudhead/journey
[0]: http://github.com/flatiron/director
[1]: http://github.com/cloudhead/cradle
[2]: http://github.com/indexzero/winston
[3]: http://github.com/substack/node-optimist
Expand Down
2 changes: 1 addition & 1 deletion lib/00getting-started/pinpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ exports.createServer = function (port) {
exports.start = function (options, callback) {
var server = exports.createServer(options.port);
callback(null, server);
}
};
27 changes: 13 additions & 14 deletions lib/01routing/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,51 @@
*
*/

var journey = require('journey');
var director = require('director');

/**
* Creates the RESTful router for the pinpoint web service
*/
exports.createRouter = function () {
var router = new (journey.Router)({
var router = new director.http.Router().configure({
strict: false,
strictUrls: false,
api: 'basic'
async: true
});

router.path(/\/bookmarks/, function () {
//
// LIST: GET to /bookmarks lists all bookmarks
//
this.get().bind(function (res) {
res.send(501, {}, { action: 'list' });
this.get(function () {
this.res.send(501, {}, { action: 'list' });
});

//
// SHOW: GET to /bookmarks/:id shows the details of a specific bookmark
//
this.get(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
res.send(501, {}, { action: 'show' });
this.get(/\/([\w|\d|\-|\_]+)/, function (id) {
this.res.json(501, { action: 'show' });
});

//
// CREATE: POST to /bookmarks creates a new bookmark
//
this.post().bind(function (res, bookmark) {
res.send(501, {}, { action: 'create' });
this.post(function (bookmark) {
this.res.json(501, { action: 'create' });
});

//
// UPDATE: PUT to /bookmarks updates an existing bookmark
//
this.put(/\/([\w|\d|\-|\_]+)/).bind(function (res, bookmark) {
res.send(501, {}, { action: 'update' });
this.put(/\/([\w|\d|\-|\_]+)/, function (bookmark) {
this.res.json(501, { action: 'update' });
});

//
// DELETE: DELETE to /bookmarks/:id deletes a specific bookmark
//
this.del(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
res.send(501, {}, { action: 'delete' });
this.del(/\/([\w|\d|\-|\_]+)/, function (id) {
this.res.json(501, { action: 'delete' });
});
});

Expand Down
40 changes: 22 additions & 18 deletions lib/02couchdb/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,87 @@
*
*/

var journey = require('journey'),
var director = require('director'),
helpers = require('./helpers');

/**
* Creates the RESTful router for the pinpoint web service
* @param {bookmark} resource: Instance of a bookmark resource
*/
exports.createRouter = function (resource) {
var router = new (journey.Router)({
var router = new director.http.Router().configure({
strict: false,
strictUrls: false,
api: 'basic'
async: true
});

router.path(/\/bookmarks/, function () {
//
// LIST: GET to /bookmarks lists all bookmarks
//
this.get().bind(function (res) {
this.get(function () {
var res = this.res;
resource.list(function (err, bookmarks) {
if (err) {
return res.send(500, {}, { error: err.error });
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmarks: bookmarks });
res.json(200, { bookmarks: bookmarks });
});
});

//
// SHOW: GET to /bookmarks/:id shows the details of a specific bookmark
//
this.get(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
this.get(/\/([\w|\d|\-|\_]+)/, function (id) {
var res = this.res;
resource.show(id, function (err, bookmark) {
if (err) {
return res.send(500, {}, { error: err.error });
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmark: bookmark });
res.json(200, { bookmark: bookmark });
});
});

//
// CREATE: POST to /bookmarks creates a new bookmark
//
this.post().bind(function (res, bookmark) {
this.post(function (bookmark) {
var res = this.res;
resource.create(bookmark, function (err, result) {
if (err) {
return res.send(500, {}, { error: err.error });
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmark: result });
res.json(200, { bookmark: result });
});
});

//
// UPDATE: PUT to /bookmarks updates an existing bookmark
//
this.put(/\/([\w|\d|\-|\_]+)/).bind(function (res, id, bookmark) {
this.put(/\/([\w|\d|\-|\_]+)/, function (id, bookmark) {
var res = this.res;
resource.update(id, bookmark, function (err, updated) {
if (err) {
return res.send(500, {}, { error: err.error });
return res.json(500, { error: err.error });
}

res.send(200, {}, { updated: updated });
res.json(200, { updated: updated });
});
});

//
// DELETE: DELETE to /bookmarks/:id deletes a specific bookmark
//
this.del(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
var res = this.res;
resource.destroy(id, function (err, destroyed) {
if (err) {
return res.send(500, {}, { error: err.error });
return res.json(500, { error: err.error });
}

res.send(200, {}, { destroyed: destroyed });
res.json(200, { destroyed: destroyed });
});
});
});
Expand Down
14 changes: 7 additions & 7 deletions lib/03authentication/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

var journey = require('journey');
var director = require('director');

/**
* randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
Expand Down Expand Up @@ -51,21 +51,21 @@ var auth = exports.auth = {
authorization = request.headers.authorization;

if (!authorization) {
return callback(new journey.NotAuthorized("Authorization header is required."));
return callback(new director.http.NotAuthorized("Authorization header is required."));
}

var parts = authorization.split(" "), // Basic salkd787&u34n=
scheme = parts[0], // Basic
credentials = base64.decode(parts[1]).split(":"); // admin:password

if (scheme !== "Basic") {
return callback(new journey.NotAuthorized("Authorization scheme must be 'Basic'"));
return callback(new director.http.NotAuthorized("Authorization scheme must be 'Basic'"));
}
else if(!credentials[0] && !credentials[1]){
return callback(new journey.NotAuthorized("Both username and password are required"));
else if (!credentials[0] && !credentials[1]){
return callback(new director.http.NotAuthorized("Both username and password are required"));
}
else if(credentials[0] !== auth.username || credentials[1] !== auth.password) {
return callback(new journey.NotAuthorized("Invalid username or password"));
else if (credentials[0] !== auth.username || credentials[1] !== auth.password) {
return callback(new director.http.NotAuthorized("Invalid username or password"));
}

// Respond with no error if username and password match
Expand Down
117 changes: 60 additions & 57 deletions lib/03authentication/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,95 @@
*
*/

var journey = require('journey'),
var director = require('director'),
helpers = require('./helpers');

/**
* Creates the RESTful router for the pinpoint web service
* @param {bookmark} resource: Instance of a bookmark resource
*/
exports.createRouter = function (resource) {
var router = new (journey.Router)({
var router = new director.http.Router().configure({
strict: false,
strictUrls: false,
api: 'basic',
filter: helpers.auth.basicAuth
async: true
});

//
// Authentication: Add a before() method to perform HTTP Basic Auth
//
router.before(/\/bookmarks/, helpers.auth.basicAuth);

//
// Resource: Bookmarks
//
router.path(/\/bookmarks/, function () {
//
// Authentication: Add a filter() method to perform HTTP Basic Auth
// LIST: GET to /bookmarks lists all bookmarks
//
this.filter(function () {
//
// LIST: GET to /bookmarks lists all bookmarks
//
this.get().bind(function (res) {
resource.list(function (err, bookmarks) {
if (err) {
return res.send(500, {}, { error: err.error });
}
this.get(function () {
var res = this.res;
resource.list(function (err, bookmarks) {
if (err) {
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmarks: bookmarks });
});
res.json(200, { bookmarks: bookmarks });
});
});

//
// SHOW: GET to /bookmarks/:id shows the details of a specific bookmark
//
this.get(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
resource.show(id, function (err, bookmark) {
if (err) {
return res.send(500, {}, { error: err.error });
}
//
// SHOW: GET to /bookmarks/:id shows the details of a specific bookmark
//
this.get(/\/([\w|\d|\-|\_]+)/, function (id) {
var res = this.res;
resource.show(id, function (err, bookmark) {
if (err) {
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmark: bookmark });
});
res.json(200, { bookmark: bookmark });
});
});

//
// CREATE: POST to /bookmarks creates a new bookmark
//
this.post().bind(function (res, bookmark) {
resource.create(bookmark, function (err, result) {
if (err) {
return res.send(500, {}, { error: err.error });
}
//
// CREATE: POST to /bookmarks creates a new bookmark
//
this.post(, function (bookmark) {
var res = this.res;
resource.create(bookmark, function (err, result) {
if (err) {
return res.json(500, { error: err.error });
}

res.send(200, {}, { bookmark: result });
});
res.json(200, { bookmark: result });
});
});

//
// UPDATE: PUT to /bookmarks updates an existing bookmark
//
this.put(/\/([\w|\d|\-|\_]+)/).bind(function (res, id, bookmark) {
resource.update(id, bookmark, function (err, updated) {
if (err) {
return res.send(500, {}, { error: err.error });
}
//
// UPDATE: PUT to /bookmarks updates an existing bookmark
//
this.put(/\/([\w|\d|\-|\_]+)/, function (id, bookmark) {
var res = this.res;
resource.update(id, bookmark, function (err, updated) {
if (err) {
return res.json(500, { error: err.error });
}

res.send(200, {}, { updated: updated });
});
res.json(200, { updated: updated });
});
});

//
// DELETE: DELETE to /bookmarks/:id deletes a specific bookmark
//
this.del(/\/([\w|\d|\-|\_]+)/).bind(function (res, id) {
resource.destroy(id, function (err, destroyed) {
if (err) {
return res.send(500, {}, { error: err.error });
}
//
// DELETE: DELETE to /bookmarks/:id deletes a specific bookmark
//
this.del(/\/([\w|\d|\-|\_]+)/, function (id) {
var res = this.res;
resource.destroy(id, function (err, destroyed) {
if (err) {
return res.json(500, { error: err.error });
}

res.send(200, {}, { destroyed: destroyed });
});
res.json(200, { destroyed: destroyed });
});
});
});
Expand Down
Loading

0 comments on commit 61ae523

Please sign in to comment.