towel
A flexible content management system written in and for node.js !
Install
Prerequisite:
- Node.js (http://nodejs.org/)
- MongoDB (http://www.mongodb.org/)
$ sudo npm install -g towel
Guide
Towel is meant as an executeable to "run" your projects.
A project is structured as follows:
└─ "root/" ├─ "models/" // folder for models, describing behaviour ├─ "templates/" // folder for ".jade" template files ├─ "static/" // static resources, like stylesheets │ ├─ "routes.json" // describes which model handels which route └─ "config.json" // configuration file
1.1) config.json
"mongodb": "localhost/test" // connection string for mongodb client "port": 80 // port for express web server
1.2) routes.json
"/": "model": "blog" // bind model "blog" to "/" "name": "Blog" // and name it "Blog", to be used when rendering layouts // Hint: "name" can be used to generate site navigations "/avatars": "avatars" // shorthand for { "model": "avatars" } // name is optional and can be omitted
2) Models
Models describe behaviour and are defined in .js files.
You can define a Model by creating a file like "blog.js"
in the "models/" folder.
~/model/blogjs module { // First we need to define a collection. // As an example we create a collection named "blog", // that has a name of type string and a post of type markdown. // Every collection also has some autogenerated fields: // - "_id" which is a MongoDB ObjectID // - "_created" which is the creation date of an entry // - "_modified" which is the last modification date of an entry // Now we need to define a routes to be handled. // These will be called relative to the route we define in "routes.json". // Every route has 3 arguments: // a raw express.js request, collections and a method to serve data. // To retrieve and manipulate data one can simply call a collection by name // and use standard mongodb methods like find, findOne and insert. // Example: render the last 10 posts in a blog }
2.1) Model Arguments
-
collection(name: string, structure: object, [(optional) significantKey: string])
creates a collection, that validates a given structure.
If a significant key is specified, it will be shown in backend list. -
route(url: string, handler: function)
creates a route in the context of a modelserve(template: string, data: object)
renders a jade templateserve.send(data: string, [(optional) mime: string])
sends raw data and sets a mime type if givenserve.sendFile(path: string)
sends a file
-
types
is an object of available collection types- number
- date
- string
- text
- markdown
-
objectid
is just MongoDB.ObjectID passed as an argument
3) Templates
Templates are .jade
files in the "templates/" folder. (Read more about jade on http://jade-lang.com/).
From within a jade template you have access to three variables: views
, view
and data
.
-
views
is a list of all routes, that have a name. Each entry has two attribues.view.base
is the url specified inroutes.json
view.model
is the name of the handling model, also specified inroutes.json
-
view
is an entry ofviews
, that matches the current site. -
data
is the data passed viaserve(...)
(see: Model Parameters)
4) Usage
First you need to add a user to the database:
$ towel -p "/path/of/project" adduser
then you can start up the service with
$ towel -p "/path/of/project" start
now a webinterface is accessible at http://host:port/towel
License
The MIT License (MIT) Copyright (c) 2015 Lukas Dietrich Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.