-
Notifications
You must be signed in to change notification settings - Fork 1
acre API
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
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
void
var acre = require('acre');
var mongoose = require('mongoose');
var express = require('express');
var app = express();
...
acre.init(mongoose, app, {putIsCreate: false, appName: 'myEpicApp'});
This method allows users to specify a function to run before a acre handles a given route
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
});
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
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
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([]);
});
Use this method to specify what routes to be COMPLETELY ignored by acre
operation - CRUD operation (see below for operations)
paths - express routes, if this is a single string then just this route is used
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'
]);
Use this method to specify what routes should only be accessible by an administrator
operation - CRUD operation (see below for operations)
paths - express routes, if this is a single string then just this route is used
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'
]);
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 '/')
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
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
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'
Tells acre what the name of your application is.
default - 'Acre App'
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
Operation enums are listed below. These are necessary because the operations do not always map 1:1 to HTTP methods
Refers to a CREATE operation. Usually a HTTP PUT or POST depending on how the putIsCreate option is set
acre.CREATE
Refers to a RETRIEVE operation. Corresponds to a HTTP GET
acre.RETRIEVE
Refers to an UPDATE operation. Usually a HTTP POST or PUT depending on how the putIsCreate option is set
acre.UPDATE
Refers to a DELETE operation. Corresponds to a HTTP DELETE
acre.REMOVE