Skip to content

nabriski/Ys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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});

About

node.js web micro framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published