crazy

An almost-web-framework. Use more Nginx!

npm install crazy
7 downloads in the last month

Setting up Nginx

Crazy Train isn’t a static file server and should always be reverse-proxied. Here’s an Nginx configuration snippet to serve up your static files:

location = / {
    proxy_pass http://127.0.0.1:8000;
}

location / {
    root /path/to/your/project/public;
    try_files $uri $uri/ @proxy;
}

location @proxy {
    proxy_pass http://127.0.0.1:8000;
}

This is always going to be more efficient and reliable than serving your static files using Node, so get used to developing with it, because you can change things without reloads, too.

If setting up a development Nginx given the above takes you more than about three minutes, you may be using the wrong operating system. Or are possibly not the fastest typist in the world.

Building an app

Create a reasonable directory structure for your project. This is the one I use:

$ mkdir project/{,public/{,stylesheets,scripts,images,fonts},templates}

Then you’ll set up some initial dependencies. May I recommend:

$ cd project
$ ed
i
{
    "dependencies": {
        "pg": "~2.8.2",
        "crazy": "0.1.0",
        "razorleaf": "~1.6.0"
    }
}
.
wq package.json
$ npm install

Now you can write your server!

$ ed
i
"use strict";

var fs = require("fs");
var pg = require("pg");
var path = require("path");
var crazy = require("crazy");
var razorleaf = require("razorleaf");

function readTemplate(name) {
    var filename = path.join(__dirname, "templates", name + ".leaf");

    return fs.readFileSync(filename, "utf8");
}

function loadTemplate(name) {
    return razorleaf.compile(readTemplate(name), { include: readTemplate });
}

var app = new crazy.Application();

var templates = {
    home: loadTemplate("home")
};

app.get("/", function(request, response, url) {
    response.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
    response.end(templates.home());
});

app.listen(8000, "127.0.0.1");
.
w server.js
,d
i
doctype

html
    head
        meta charset: "utf-8"

        title "Hello, world!"

    body
        h1 "It works!"
.
w templates/home.leaf
$ node server

Yes, this is a little verbose for now, but wait while Crazy Train gets started! In the meantime, it’s a good idea to read the documentation for the libraries used here. Know what every part does at all times!

npm loves you