Created
August 20, 2015 22:05
-
-
Save jimthedev/21b194a7c7a93bbdd25d to your computer and use it in GitHub Desktop.
Falcor with express featuring split route files and route access logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
// ... SNIP: You should include and wire up express as needed before this | |
// ... | |
// Falcor deps | |
var falcor = require('falcor'); | |
var falcorExpress = require('falcor-express'); | |
var bodyParser = require('body-parser'); | |
// ROUTES | |
var router = require('./router.js'); | |
// Set up the body to return json | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
// Add a static public route for your front end index.html | |
app.use(express.static('src/public')); | |
// Falcor middleware | |
app.use('/model.json', falcorExpress.dataSourceRoute(function (req, res) { | |
// NOTICE: you should change this line to get the user ID from the session | |
var routerInstance = new router('MyUserId'); | |
return { | |
// Wrap the falcor router so that we can | |
// output logging information | |
get: function(paths) { | |
return routerInstance.get(paths) | |
.doAction(function(output) { | |
console.log(routerInstance.userId + ' requested paths ' + JSON.stringify(paths)); | |
console.log('And received:' + JSON.stringify(output, null, 2)); | |
}); | |
}, | |
set: function(jsong) { | |
return routerInstance.set(jsong) | |
.doAction(function(output) { | |
console.log(routerInstance.userId + ' set the following jsong ' + JSON.stringify(jsong)); | |
console.log('And received:' + JSON.stringify(output, null, 2)); | |
}); | |
}, | |
call: function(callPath, args, suffixes, paths) { | |
return routerInstance.call(callPath, args, suffixes, paths) | |
// .doAction(function(output) { | |
// console.log('a call was made', arguments); | |
// }); | |
} | |
}; | |
})); | |
// ... | |
// ... SNIP: You should start express listening after this | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Router = require('falcor-router'); | |
var routes = require('./routes'); | |
// Create a Router base class and load up our routes | |
var VetPetRouterBase = Router.createClass(routes.load(this)); | |
var VetPetRouter = function(userId){ | |
// Invoking the base class constructor | |
VetPetRouterBase.call(this) | |
this.userId = userId; | |
console.log('a call was made with user id:' + this.userId); | |
}; | |
// Creating a derived class using JavaScript's classical inheritance pattern | |
VetPetRouter.prototype = Object.create(VetPetRouterBase.prototype); | |
module.exports = function(userId) { | |
return new VetPetRouter(userId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This files loads, combines, and exports an array of routes | |
module.exports.load = function(router) { | |
return [].concat( | |
require('./routes.veterinarians.js'), | |
require('./routes.pets.js') | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Export an array of pet routes | |
module.exports = [ | |
{ | |
route: 'pets[{integers:petIds}]["name"]', | |
get: function(pathSet) { | |
// .... Get information from the pets service | |
console.log('a "get" was made by user id:' + this.userId); | |
} | |
}, | |
{ | |
route: 'pets[{integers:petIds}]["rating"]', | |
get: function(pathSet) { | |
// .... Get information from the pet rating service | |
} | |
} | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Export an array of pet routes | |
module.exports = [ | |
{ | |
route: 'veterinarians[{integers:veterinarianIds}]["name"]', | |
get: function(pathSet) { | |
// .... Get information from the veterinarian service | |
} | |
}, | |
{ | |
route: 'veterinarians[{integers:veterinarianIds}]["rating"]', | |
get: function(pathSet) { | |
// .... Get information from the veterinarian rating service | |
} | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment