From 443bee744a867fa116ebe45f55cdedfef77ade96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 2 Jun 2016 16:55:36 +0200 Subject: add controllers --- controllers/MovieController.js | 15 +++++++++++++++ index.js | 6 ++++++ routes.js | 3 +++ 3 files changed, 24 insertions(+) create mode 100644 controllers/MovieController.js create mode 100644 routes.js diff --git a/controllers/MovieController.js b/controllers/MovieController.js new file mode 100644 index 0000000..5cb7681 --- /dev/null +++ b/controllers/MovieController.js @@ -0,0 +1,15 @@ +var restful = require('node-restful'); + +module.exports = function(app, route) { + + var rest = restful.model( + 'movie', + app.models.movie + ).methods(['get', 'put', 'post', 'delete']); + + rest.register(app, route); + + return function(req, res, next) { + next(); + }; +}; diff --git a/index.js b/index.js index 91dbd21..5a31090 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var express = require('express'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); +var _ = require('lodash'); // create express application var app = express(); @@ -33,6 +34,11 @@ mongoose.connection.once('open', function() { app.models = require('./models/index.js'); + var routes = require('./routes.js'); + _.each(routes, function(controller, route) { + app.use(route, controller(app, route)); + }); + console.log("listening on port " + port); app.listen(port); }); diff --git a/routes.js b/routes.js new file mode 100644 index 0000000..5111993 --- /dev/null +++ b/routes.js @@ -0,0 +1,3 @@ +module.exports = { + 'movie': require('./controllers/MovieController') +}; -- cgit v1.1-2-g2b99