0.3.5 • Published 9 years ago

ys v0.3.5

Weekly downloads
5
License
MIT
Repository
github
Last release
9 years ago

Ys

Node.js micro web framework.

Installation

npm install ys

Hello World:

var Ys = require('ys').Ys;

Ys("^/$").get = function(req,res){
    res.end("Hello World!");
};

Ys.run({debug:true});

In debug mode the server reloads itself on source changes. Do not use in production.

API:

Server Run Params

Ys.run({
    host:"localhost",//default
    port:8780,//default
    user:"nabriski",//no default, will set the process's user to this user after binding to port
    partials : { // path to handlebars' partials
                "path":".",//default - path to look for partial files
                "ext":"mustache"//extension of partial files
    },
    debug:false,//default, if set to true server will restart if file including Ys has changed
    onInit:null, //default, optional callback to be called when Ys starts listening to requests
    pidFile:null, //default, optional file where pid of process is written,
    exceptionHandler : null //default, set a function to handle uncaught exceptions
});

Generic Response

Ys("^/$").get = function(req,res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end("<h1>Hello World!</h1>");
};

JSON Response

Ys("^/json$").get.json = function(req,res){
    res.returnObject({"message" : "Hello World"});
};

Templating

// return html response using template 'hello.html' 
// (default template engine is 'handlebars.js' but any template engine with a 'compile' method can be defined)
// Where 'hello.html' is:
// <h1>Hello {{name}}!</h1>
Ys("^/hello_bob$").get.template = {'hello.html':function(req,res){
    res.returnObject({"name" : "Bob"};);
}};

Echo Server

Ys("^/hello/(\\w+)/$").get = function(req,res){
    res.end("Hello "+req.$1+"!");
};

Static Files

//return static files
Ys("^/static/.*$").get.static = ".";

//return gzipped file (depends on client's accept encoding)
Ys("^/gzip/.*$").get.gzip = ".";

Redirect

Ys("^(.*/[^\./]+)$").redirect = "$1/";//adds a trailing slash when missing

Rewrite

//rewrite /json_alias/ to /json/
// must appear before the /json/ route
Ys("^/json_alias/$").rewrite = "/json/";

Ys("^/json$").get.json = function(req,res){
    res.returnObject({"message" : "Hello World"};);
};

Proxy

//proxy all requests to http://localhost:8080/
Ys("^/.*$").proxy = "http://localhost:8080/";

Running Multiple Instances

var a= Ys.instance(), b = Ys.instance();

a("^/$").get.html = function(req,res){
    res.end("<h1>Instance A</h1>");
};
a.run({port:8780};);

b("^/$").get.html = function(req,res){
    res.end("<h1>Instance B</h1>");
};
b.run({port:8781});
0.3.5

9 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.9

11 years ago

0.1.8

11 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago