Web Development with Node and Express by Ethan Brown

Web Development with Node and Express by Ethan Brown

Author:Ethan Brown
Language: eng
Format: epub, mobi, pdf
ISBN: 9781491949306
Publisher: O’Reilly Media, Inc.
Published: 2014-10-26T16:00:00+00:00


Declaring Routes in a Module

The first step to organizing our routes is getting them all into their own module. There are multiple ways to do this. One approach is to make your module a function that returns an array of objects containing “method” and “handler” properties. Then you could define the routes in your application file thusly:

var routes = require('./routes.js')(); routes.forEach(function(route){ app[route.method](route.handler); })

This method has its advantages, and could be well suited to storing our routes dynamically, such as in a database or a JSON file. However, if you don’t need that functionality, I recommend passing the app instance to the module, and letting it add the routes. That’s the approach we’ll take for our example. Create a file called routes.js and move all of our existing routes into it:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.