christroutner

routes/index.js

Oct 29th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This file is from keystonejs-proj/routes/index.js. It shows the modifications I made to open the API
  2. //based on Jed's Gist: https://gist.github.com/JedWatson/9741171#file-routes-index-js-L24
  3.  
  4. /**
  5.  * This file is where you define your application routes and controllers.
  6.  *
  7.  * Start by including the middleware you want to run for every request;
  8.  * you can attach middleware to the pre('routes') and pre('render') events.
  9.  *
  10.  * For simplicity, the default setup for route controllers is for each to be
  11.  * in its own file, and we import all the files in the /routes/views directory.
  12.  *
  13.  * Each of these files is a route controller, and is responsible for all the
  14.  * processing that needs to happen for the route (e.g. loading data, handling
  15.  * form submissions, rendering the view template, etc).
  16.  *
  17.  * Bind each route pattern your application should respond to in the function
  18.  * that is exported from this module, following the examples below.
  19.  *
  20.  * See the Express application routing documentation for more information:
  21.  * http://expressjs.com/api.html#app.VERB
  22.  */
  23.  
  24. var keystone = require('keystone');
  25. var middleware = require('./middleware');
  26. var importRoutes = keystone.importer(__dirname);
  27.  
  28. // Common Middleware
  29. keystone.pre('routes', middleware.initLocals);
  30. keystone.pre('render', middleware.flashMessages);
  31.  
  32. // Import Route Controllers
  33. var routes = {
  34.         views: importRoutes('./views'),
  35.         api: importRoutes('./api')
  36. };
  37.  
  38. // Setup Route Bindings
  39. exports = module.exports = function(app) {
  40.  
  41.         // Views
  42.         app.get('/', routes.views.index);
  43.         app.get('/blog/:category?', routes.views.blog);
  44.         app.get('/blog/post/:post', routes.views.post);
  45.         app.get('/events/', routes.views.events);
  46.         app.get('/gallery', routes.views.gallery);
  47.         app.all('/contact', routes.views.contact);
  48.  
  49.         app.get('/api/post/list', keystone.middleware.api, routes.api.posts.list);
  50.         app.all('/api/post/create', keystone.middleware.api, routes.api.posts.create);
  51.         app.get('/api/post/:id', keystone.middleware.api, routes.api.posts.get);
  52.         app.all('/api/post/:id/update', keystone.middleware.api, routes.api.posts.update);
  53.         app.get('/api/post/:id/remove', keystone.middleware.api, routes.api.posts.remove);
  54.  
  55.         app.get('/api/img/list', keystone.middleware.api, routes.api.img.list);
  56.         app.get('/api/img/:id', keystone.middleware.api, routes.api.img.get);
  57.         app.all('/api/img/:id/update', keystone.middleware.api, routes.api.img.update);
  58.  
  59.         // NOTE: To protect a route so that only admins can see it, use the requireUser middleware:
  60.         // app.get('/protected', middleware.requireUser, routes.views.protected);
  61.  
  62. };
Advertisement
Add Comment
Please, Sign In to add comment