Skip to content
johneke edited this page Aug 18, 2013 · 7 revisions

Methods

init(mongoose, app, options)

This is the main entry point into acre. Before calling this method, make sure you have set up all your schemas in mongoose and initialized them into models

parameters

mongoose - instance of mongoose used to initialize database schemas, as for now there is a hard dependency on mongoose 3.6.15

app - instance of express.js where users web app will be served

options - list of options to change acre's behaviour, please see options section below

returns

void

var acre = require('acre');
var mongoose = require('mongoose');
var express = require('express');
var app = express();

...

acre.init(mongoose, app, {putIsCreate: false, appName: 'myEpicApp'});

pre(operation, path, callback)

This method allows users to specify a function to run before a acre handles a given route

parameters

operation - CRUD operation (see below for operations)

path - express route

callback - user defined function to call, takes the form function(request, response, next){}

// this will print 'hello user' before any new users are created
acre.pre(acre.CREATE, '/users', function(request, response, next){
    console.log('hello user');
    next(); // very important line, this makes sure that acre is called next
});

post(operation, path, callback)

This method allows users to specify a function to run after a acre handles a given route, and before data is returned to the HTTP client

parameters

operation - CRUD operation (see below for operations)

path - express route

callback - user defined function to call, takes the form function(request, response, data){}, for acre.RETRIEVE operation, data will contain the fetched document. Otherwise data is null

returns

void

// this will always return an empty list of users, regardless of what's in the database
acre.post(acre.RETRIEVE, '/users', function(request, response, users){
    response.json([]);
});

override(operation, paths)

Use this method to specify what routes to be COMPLETELY ignored by acre

parameters

operation - CRUD operation (see below for operations)

paths - express routes, if this is a single string then just this route is used

returns

void

// this will make sure that acre never handles updates to all books
acre.override(acre.UPDATE, '/books');

// this will make sure that acre has nothing to do with deleting professor ratings
// and student ratings
acre.override(acre.REMOVE, [
    '/professors/:professor_id/rate',
    '/students/:student_id/rate'
]);

private(operation, paths)

Use this method to specify what routes should only be accessible by an administrator

parameters

operation - CRUD operation (see below for operations)

paths - express routes, if this is a single string then just this route is used

returns

void

// this will make sure that acre never handles updates to all books
acre.override(acre.UPDATE, '/books');

// this will make sure that acre has nothing to do with deleting professor ratings
// and student ratings
acre.override(acre.REMOVE, [
    '/professors/:professor_id/rate',
    '/students/:student_id/rate'
]);

bodyParser(app)

acre specific middleware that must be placed inline with all other express middleware during app configuration. This line must be placed just before the express body parser in your node js application

app.js

// other middleware ...
acre.bodyParser(app);
app.use(express.bodyParser());
// other middleware ...

Options

rootPath: String

Use this option to specify a prefix for all your REST URLs.

acre.init(mongoose, app, {rootPath: ''}); // REST URLS are now like /professors/id
acre.init(mongoose, app, {rootPath: '/my/awesome-api'}); // REST URLS are now like /my/awesome-api/professors/id

default - '' (empty string, represents '/')

putIsCreate: Boolean

Setting this flag to true means that for CREATE operations, the PUT method will be used, and for UPDATE operations POST method is used. Setting this flag to false means that for CREATE operations, the POST method will be used, and for UPDATE operations PUT method is used.

default - true

adminPortal: Boolean

This flag sets whether or not to enable the admin portal.

Note

The admin portal will be wide open (no authentication required) until you create the first admin account. This is very important, if you do not create an admin account, your admin portal will be wide open

default - true

adminRoute: String

Sets the URL for the admin portal.

// the following line makes the admin portal available at: http://<site-url>/my-obfuscated-admin-url
acre.init(mongoose, app, {adminRoute: '/my-obfuscated-admin-url'});

default - '/admin'

appName: String

Tells acre what the name of your application is.

default - 'Acre App'

adminPasswordSalt: String

This string is used as a seed when hashing password for acre admin users.

Note

Users MUST change this option. If not a default salt is used which everyone on earth knows since the project is open source. To avoid a gaping security hole in your site, make sure you change this option


Operations

Operation enums are listed below. These are necessary because the operations do not always map 1:1 to HTTP methods

CREATE

Refers to a CREATE operation. Usually a HTTP PUT or POST depending on how the putIsCreate option is set

acre.CREATE

RETRIEVE

Refers to a RETRIEVE operation. Corresponds to a HTTP GET

acre.RETRIEVE

UPDATE

Refers to an UPDATE operation. Usually a HTTP POST or PUT depending on how the putIsCreate option is set

acre.UPDATE

REMOVE

Refers to a DELETE operation. Corresponds to a HTTP DELETE

acre.REMOVE