dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Best practice for moving code out of app.js or server.js #101

Closed philotas closed 8 years ago

philotas commented 9 years ago

Do you move the code for creation of the resources for each Model into a classMethod of that model? I would not want to do that but have all the REST API epilogue logic such as middleware separated from the model definition and I am fine to call it "manually" from app.js. Like with models one file per resource would be nice, I guess.

How do you guys handle this?

mbroadst commented 9 years ago

It's really up to the user. I tend to do something like this:

This way I can separate all things that relate to the actual web side of things into their own files, generally starting with a call to epilogue.resource, but it may also include specialized endpoints related to the resource that epilogue doesn't automatically generate.

Sorry if that was unclear at all, let me know if this makes sense - I could probably provide a more in depth example. Maybe other people have different/better ways to do this, I'd be very interested to hear about them.

philotas commented 9 years ago

Thanks for your answer.

I get the idea. Do you use an index.js (like in the models dir) that traverses the current directory and creates the resources and optional endpoints for each file?

Would you mind sharing some code? I am quite new to node etc and my code does not work, so I would appreciate help.

in my /api/index.js I am trying to do this:

var epilogue = require('epilogue');
var app = require('../app');
var db = require('../models');

var initialize = function(){
  epilogue.initialize({
    app: app,
    sequelize: db
  });
}

module.exports = initialize;

I get this error (also when I pass the app as parameter to initialize from within app.js):

  if (app.name === 'restify' && self.method === 'delete')
         ^

TypeError: Cannot read property 'name' of undefined
    at Controller.route (/Users/...../node_modules/epilogue/lib/Controllers/base.js:78:10)

The error happens in epilogue but I am sure I am doing something wrong here.

philotas commented 9 years ago

For the record: I finally did the following and it works fine:

app.js:

var apiResources = require('./api').initialize(app);

api/index.js

'use strict';

var epilogue = require('epilogue');
var db = require('../models');

var initialize = function(app) {
  epilogue.initialize({
    app: app,
    sequelize: db.sequelize
  });

  return {
    userResource: require('./user')
  };
};

module.exports = {
  initialize: initialize
};

/api/user.js

'use strict';

var epilogue = require('epilogue');
var db = require('../models');
var permissionMiddleware = require('./user_permission');

var userResource = epilogue.resource({
  model: db.User,
  excludeAttributes: ['hash','salt','activationKey','resetPasswordKey'],
  endpoints: ['/api/users', '/api/users/:id'],
  actions: ['read','list','create'],
  sort: {
    default: 'email,username'
  }
});

userResource.use(permissionMiddleware);

module.exports = userResource;