## overview
Appex is a nodejs web service framework built on top of the TypeScript programming language. Appex
enables nodejs developers to expose typescript functions as http endpoints as well as generate meaningful service
meta data for clients to consume.
Appex also provides a dynamic compilation environment for typescript to aid in development. Appex will effeciently
manage compilation in the background without the need to restart the web server, or use additional modules.
Appex is designed to operate as both a standalone web service solution or compliment an existing application written
in frameworks such as express / connect.
### the appex runtime
The Appex runtime is compilation engine that handles compiling typescript code, mapping routes and function
invocation. Appex provides a utility method for setting up the runtime, as described below.
var http = require('http');
var appex = require('appex');
var runtime = appex.runtime ( { sourcefile : './program.ts' } );
var server = http.createServer( runtime );
server.listen(3000);
The appex.runtime() method returns a http handler function which is both compatable with nodejs'
http server as well as connect middleware. This is the recommended approach of creating runtimes,
However, if you need to access the runtime directly or are simply curious, you can also setup
the runtime as follows..
var http = require('http');
var appex = require('appex');
var runtime = new appex.Runtime ( { sourcefile : './program.ts' } );
var server = http.createServer( function(req, res) {
console.log(runtime);
runtime.request_handler(req, res, function() {
});
});
server.listen(3000)
### runtime options
The appex runtime accepts the following options.
var options = {
sourcefile : './program.ts',
devmode : true,
logging : true,
stdout : process.stdout,
stderr : process.stderr,
};
var runtime = appex.runtime ( options );
### appex json handlers
A appex json handler is a function suited to handling json based http requests. appex json handlers
are invoked via HTTP POST and expect JSON to be subbmited with the request. Passing null or invalid
JSON results in the request argument being null.
A appex json handler requires the following signature.
- argument[0] - the appex context
- argument[1] - A optionally typed json request object.
- argument[2] - a optionally typed optypescript callback with a single argument for the object response.
- returns - void (optional)
The return type is optional. json handler functions "must" invoke the callback to complete the request.
export function method(context, request, callback:(response) => void) : void {
callback(request);
}
### exporting_functions
Appex extends TypeScripts concept of visibility to include visibility over http. From this
developers and control which functions are exported as http handlers.
In order to make a function accessible over http, you must explicitly "export" this function.
Consider the following example:
module private_module {
export function public_method () { }
function private_method() { }
}
function private_function() { }
export function public_function (context) {
private_function();
private_module.public_method();
context.response.write('testing');
context.response.end();
}
The above will result in the following route being created:
http:
### wildcard_functions
Appex supports typed url arguments on functions named 'wildcard'. Wildcard functions are special in the regard
they support more than one argument (other than the context) for which url parameters will be mapped.
Appex currently supports only numeric type annotations on wildcard arguments. All other types will mapped as strings.
if a argument is annotated with 'number', then only numeric are matched.
export module blogs {
export function index(context) {
context.response.write('blogs index')
context.response.end();
}
export function wildcard(context, year:number, month:number, day:number) {
context.response.write('blogs ' + year + ' ' + month + ' ' + day)
context.response.end();
}
}
note: wildcard functions should be declared last in any module scope. this way, other function types in this scope
will be resolved first.
### development_mode
var runtime = appex.runtime ({ sourcefile : './program.ts', devmode : true, logging: true });
Appex is built directly on top of the Microsoft TypeScript 0.9 compiler and leverages it for tight
integration with the nodejs platform. By enabling the 'devmode' option, Appex will efficiently
rebuild your source code on each request made to the server.
Appex achieves performance in this regard by leveraging features available in
TypeScript compiler which facilitate incremental building / caching of typescript
compilation units.
In addition to this, compilations are run as a background worker process to ensure they
do interupt requests being served on the parent web process.
Appex will output syntax and type errors to the stdout and http response. Syntax errors
will not bring down the web process. And you won't need to restart on code updates.
### structuring projects
Appex leverages TypeScript's ability to reference source files with the 'reference' element. Appex
will traverse each source files references and include it as part of the compilation.
Developers can use this functionality to logically split source files into reusable components of
functionality, as demonstrated below.
var appex = require('appex');
var runtime = appex.runtime ({ sourcefile : './index.ts' });
require('http').createServer( runtime ).listen(3000);
export function index (context) { }
export function about (context) { }
export function contact (context) { }
export module users {
export function login (context) { }
export function logout (context) { }
}