Servers

tmcw edited this page Feb 14, 2012 · 3 revisions

Servers

Servers are a way to add HTTP endpoints to your application that aren't part of the client-side Backbone application (e.g. API endpoints), as well as serving Backbone controller routes. Servers can be mounted onto other servers. They are instances of express' HTTPServer, with some added functionality:

  • Servers have a initialize() method that is called when a server instance is created. Different instances of a server can be mounted on other servers.
  • When a server instance has a port property, it'll listen on that port when the application is started. That means that you have servers that only exist to be mounted on other servers, while some servers are root servers that will actually be started.

Default servers

Bones provides a set of useful default servers, defined in the servers directory:

Asset.bones

Exposes each plugin's assets folder using express' static middleware.

Given an assets path like

assets/css/reset.css

The bones default middleware will serve reset.css from

/assets/YOURAPPNAME/css/reset.css

Given YOURAPPNAME taken from the package.json 'name' attribute.

  • Middleware.bones: Adds commonly used middleware (cookie parser, body decoder, CSRF protection etc.)

  • Route.bones: Serves Backbone, jQuery and other Bones code required for applications to run in the browser. It also provides endpoints for client-side controllers, models, views and templates, as well as:

    • /api/:collection: Any collection registered with that server has this endpoint

    • /api/:model/:id: Any model registered with that server has this endpoint

      This essentially means that you never have to register routes for any model you create; it'll just work automatically.

  • Core.bones: Default root level server that has the previous three servers mounted by default. It is generally a good idea to mount other servers onto this servers as opposed to creating your own root-level server.