Last active
August 29, 2015 14:02
-
-
Save tybenz/f3ec2b8e0fabd1f78335 to your computer and use it in GitHub Desktop.
Restify app **after** paper-router. Read comments below to get an idea of how it works.
This file contains hidden or 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 restify = require('restify'); | |
var PaperRouter = require( 'paper-router' ); | |
var routes = require( './routes' ); | |
var server = restify.createServer({ | |
name: 'luca', | |
}); | |
server.use( function crossOrigin( req, res, next ) { | |
res.header( "Access-Control-Allow-Origin", "*" ); | |
res.header( "Access-Control-Allow-Headers", "X-Requested-With" ); | |
return next(); | |
}); | |
server.use( restify.acceptParser( server.acceptable ) ); | |
server.use( restify.queryParser() ); | |
server.use( restify.bodyParser() ); | |
server.pre( restify.pre.sanitizePath() ); | |
// | |
// Using paper router to separate out controllers from routes | |
// Routes are read in from a separate file (see routes.js below) | |
// | |
var router = new PaperRouter( server, __dirname + '/controllers', routes ); | |
server.listen( 8000, function() { | |
console.log( "%s listening at %s", server.name, server.url ); | |
}); |
This file contains hidden or 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 BananaModel = require( '../models/banana.js' ); | |
// Controllers are just plain objects with actions as properties | |
// Use typical CRUD action names to line up with paper-router's | |
// resourceful routing (see routes.js) | |
var BananaController = { | |
index: function( req, res, next ) { | |
res.send( BananaModel.getAll() ); | |
} | |
create: function( req, res, next ) { | |
var banana = BananaModel.new({ | |
weight: req.params.weight, | |
length: req.params.length, | |
}) | |
.save(); | |
res.send( banana ); | |
} | |
}; | |
module.exports = BananaController; |
This file contains hidden or 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 HomeController = { | |
index: function( req, res, next ) { | |
res.send( { message: 'You are home' } ); | |
} | |
}; | |
module.exports = HomeController; |
This file contains hidden or 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 PeelModel = require( '../models/peel.js' ); | |
var PeelsController = { | |
index: function( req, res, next ) { | |
var peels = PeelModel.where( { banana_id: req.params.banana_id } ); | |
res.send( peels ); | |
} | |
}; | |
module.exports = PeelsController; |
This file contains hidden or 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
module.exports = function( router ) { | |
// GET '/' => controller: 'home', action: 'index' | |
router.get( '/', 'home#index' ); | |
// Creates the following routes but only if the actions exist | |
// GET '/bananas' => controller: 'bananas', action: 'index' | |
// GET '/bananas/:id' => controller: 'bananas', action: 'show' | |
// GET '/bananas/new' => controller: 'bananas', action: 'new' | |
// GET '/bananas/:id/edit' => controller: 'bananas', action: 'edit' | |
// POST '/bananas' => controller: 'bananas', action: 'create' | |
// PUT '/bananas/:id' => controller: 'bananas', action: 'update' | |
// DELETE '/bananas/:id' => controller: 'bananas', action: 'destroy' | |
router.resources( 'bananas' ); | |
// Creates the following routes but only if the actions exist | |
// GET '/bananas/:banana_id/peels' => controller: 'peels', action: 'index' | |
// GET '/bananas/:banana_id/peels/:id' => controller: 'peels', action: 'show' | |
// GET '/bananas/:banana_id/peels/new' => controller: 'peels', action: 'new' | |
// GET '/bananas/:banana_id/peels/:id/edit' => controller: 'peels', action: 'edit' | |
// POST '/bananas/:banana_id/peels' => controller: 'peels', action: 'create' | |
// PUT '/bananas/:banana_id/peels/:id' => controller: 'peels', action: 'update' | |
// DELETE '/bananas/:banana_id/peels/:id' => controller: 'peels', action: 'destroy' | |
router.resources( 'peels', '/bananas/:banana_id' ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment