-
Notifications
You must be signed in to change notification settings - Fork 3
creating a brainy application
although brainy encompasses many tools (brainy-sync, brainy-api), this article focuses on using brainy-server which leverages all of the brainy components.
the only system-level dependency is mongodb. this requires you have an instance of mongod
running prior to moving forward. because brainy connect to 127.0.0.1: 27017
by default no configuration should be required.
start by installing the brainy-server, cloning brainy-boilerplate, and running the brainy server.
$ npm install brainy-server -g
$ git clone [email protected]:brainyio/brainy-boilerplate.git
$ cd brainy-boilerplate/src
$ brainy-server
navigate to http://localhost:8000/. you will see a blank page, with "hello world" printed in the developer console. this means we are ready to start building our app.
brainy relies on some file structure, but tries to stay out of the way. it only cares about two things: where your javascript is located (a single directory), and where your resources are located (a single directory). by default these are js
and js/resources
. the boilerplate respects these defaults, although it contains not contain any resources, you will define your own. you can configure the directories brainy-server searches with the paths.js
and paths.resources
options respectively.
naturally, index.html is the entry point to your application, and you are free to structure your application as necessary. with the afforementioned configuration in mind, your application architecture is up to you.
a resource is any Backbone model or collection. these can be used on the client as expected, and are used by brainy-server to set up an api. brainy depends on the urlRoot
or url
property of a collection or model respectively to infer an HTTP endpoint. a simple model definition looks like this (put it in js/resources/post.js
)
define([
'backbone'
], function(Backbone) {
return Backbone.Model.extend({
idAttribute: '_id',
urlRoot: '/posts'
});
});
restart the brainy-server process and try out the following CURL calls:
$ curl -XPOST -d 'text=my first post' http://localhost:8000/posts
{ "text": "my first post", "_id": "5128a82a9b86bc14af000001" }
$ curl http://localhost:8000/posts/5128a82a9b86bc14af000001
{ "text": "my first post", "_id": "5128a82a9b86bc14af000001" }
brainy has created an api based on your resources. it supports all HTTP methods, models and collections, and arbitrary mongodb queries.
you have a model and a way to save it and stuff, you know what to do next. make some views and make them do things.