Build a blog with Areto Node.js framework

File controller

The FileController class defines a controller of the file model.

To turn off unnecessary actions inherited from the base controller, certain properties is set to NULL values in the class constants.

modules/admin/controllers/FileController.js

'use strict';                            
const Base = require('../components/CrudController'); 
module.exports = class FileController extends Base {

  static getConstants () {
    return {
      METHODS: {
        'upload': ['post']
      },
      actionCreate: null,
      actionUpdate: null
    };
  }

  getModelClass () {
    return require('../models/File');
  }
  // place methods here
};
module.exports.init(module);

The actionUpload action is limited to the POST request by the METHODS constants. The 404 error will be returned for all other request's types of this action.

modules/admin/controllers/FileController.js

...
actionUpload () {
  let model = new (this.getModelClass());
  model.upload(this, err => {
    if (err) {
      this.throwError(err);
    } else if (model.hasError()) {
      this.sendText(this.translate(model.getFirstError()), 400);
    } else {
      this.sendText(model.getId());
    }
  });
}
...