fatless v0.0.4
Fatless
Fatless is a small Framework aimed at being modular. It exposes a small API but a lot of utilities, and some modules are already included so you don't need to install them. Fatless is promise-based, using q.
npm install fatless --saveHere's an example, very simple "Hello, World" app:
require("fatless")(__dirname);
R.route("/", [], function(req, res) {
res.body = "Hello, World!";
});
F
.start({
port: 5000
})
.then(function() {
console.log("Started !");
});Let's go over it line by line:
require("fatless")(__dirname);The Fatless module exposes a function that creates all the global variables. These are Fatless, Router, Utils and Q (the q lib). The function requires a parameter, the root directory of the application.
R.route("/", [], function(req, res) {route is a method from the Router. It allows to create a route with any path/method. The first parameter is the path, the second is the middleware and the third a callback. Fatless uses path-to-regexp to match parameters, which you can find in req.params. If you want to change the HTTP Method, prepend it to the path (ex: POST /account/create).
res.body = "Hello, World!";res, an instance of Response, exposes a body variable. The data will only be sent when the request completely ends. Should you do asynchronous stuff, make sure to return a promise that resolves when body is set, or the response would already be sent.
F
.start({
port: 5000
})
.then(function() {
console.log("Started !");
});Ok, so we'll split this. First of, we call F(ramework).start, which takes an argument: the application configuration. One thing you need to supply is a port. start returns a promise which will resolve when all modules are loaded and the web server listening. It will reject on any error during initialisation.
Then, because start returns a q promise, we can make a function wait for it to be done using then. We highly recommend you to read the Q API reference and to get used to it.
That should get you started on the most basic applications. If you need more, read the API !
API
This is still incomplete, and the API is pre-alpha.
F
F(atless) is the global framework variable. It doesn't contain much.
Propriétés:
config: Configuration you passed toF.start(), merged with the default configuration (F._defaultConfig, don't touch that).
Méthodes:
start(config): Starts the app * Returns: a promise
R
R(outer) is the application router. It can define classic routes, middleware and error handlers.
Méthodes:
route(path [, name], middleware, handler): Adds a route. All middleware will be called in that order.middleware(name, handler): Adds a middleware.handlermay return a promise.error(code, handler): Adds an error handler.- TODO:
test(path, req): Makes an internal request to path, using the provided request. * Returns: a promise that resolves withreqandres.
All handlers (route, middleware, error) are called like this: handler(request, response). Error handlers also have req.code, the error code.
When creating routes (R.route), the format of path can be:
[METHOD] [PATH][PATH](the method will beGET)
Exemples:
GET /blog/blog(Same all the one up)POST /blog/article/create
U
U(tils) contient des fonctions utilitaires ainsi que des librairies comme fs-extra et path.
Propriétés:
Méthodes:
merge: deep-merges the arguments. Uses deepmerge.endsWith(str, suffix)extendClass(A, withB)isArray(a)isObject(o)isString(s)isFunction(f)isError(e)- TODO:
list(dir): List files & directories in a directory. Returns a promise. - TODO:
listSync(dir): Synchronous version oflist(dir) - TODO:
listFiles(dir): List files in a directory. Returns a promise. - TODO:
listFilesSync(dir): Synchronous version oflistFiles(dir) - TODO:
listDirs(dir): List directories in a directory. Returns a promise. - TODO:
listDirsSync(dir): Synchronous version oflistDirs(dir)
License
Fatless is MIT-licensed. More information in README.md.