Chilla: Make your Node apps Representational
I want you to make RESTful applications
But what does that mean? What makes a RESTful application? Well, for starters, you have to define what resources you want to expose. Then, how to represent them. This module aims to help you with the Representational part.
Principles that this Codebase Intends to Follow
- Separation of Concerns
- Encapsulate what varies
- Single Responsibility
Key Ideas that this Codebase Intends to Implement
- Content Negotation
- The Representational part of REST
First of all, the core module is Represent and located in represent.js.
node app.js (or npm start - requires nodemon) starts the server and in there is where the Represent module is being used.
Chilla.themeRoot is set to the path where it can find it's templates. The term theme here comes from Wordpress' themes facility where you can define a theme for a site with it's CSS, JS, and templates. A template is an HTML, XML, JSON, etc. file. Technically, whatever Content Type exists in the contenTypes folder.
Chilla.contenTypesFolder is set to the path where the content type modules are located.
Chilla.endpoints represent a list of modules that implement "handles" and "execute" and that will respond to a URI request. In this codebase, all of the endpoints are located in the resources folder (think of things as resources). But don't focus on that. Focus on the part where Represent.execute is called because that's the important part.
When Chilla.execute is finally called (in the app.js file), it implements Content Negotiation by checking for a file extension specified in the URI path (e.g. index.html, index.json, index.xml). If there's no file extension specified, then it checks the accepts header. If the code can't determine the requested content type, then it responds with Not Accepted. If it can, then the code searches it's contentTypes object (set by controlling code) for every module that indicates it wants to handle the requested content type. The code builds the template path by calling resource.getTemplateFor and sends it as one of the parameters to contentType.execute. The content type module reads the file, renders it via EJS and calls the callback function with the output, which is then sent to response.end
Ok. So now what can I do with this thing?
I really have to create some examples to get my point across, so I'll be doing that in the next months.