partial.js logo
Latest version
v1.3.1 (RC2)

Framework


The main class (core) of partial.js. This class is singleton and is set as global variable.


console.log(framework.version);

// In a defintion, module, etc.:
framework.on('load', function() {

	// ...
	// ...

});

Properties


framework.async {Async}framework.cache {FrameworkCache}framework.config {FrameworkConfig}framework.connections {Object}framework.controllers {Object}framework.databases {Object}framework.errors {Array}framework.fs {FrameworkFileSystem}framework.global {Object}framework.helpers {Object}framework.ip {String}framework.path {FrameworkPath}framework.port {Number}framework.resources {Object}framework.restrictions {FrameworkRestrictions}framework.server {Object}framework.stats {FrameworkStats}framework.temporary {Object}framework.version {Number}framework.versionNode {Number}framework.versions {Object}framework.wokers {Object}
framework.async;
readonly
return Async;

Property return Async object for asynchronous work.

framework.cache;
readonly

Framework cache for storing custom values in the memory.

framework.config;
readonly

Property return framework configuration. Property is readonly and it's has refreshed every 20 minutes.

framework.connections;Default: {}
return Object;

This property storing websocket connections. Each property in this object contain URL address into the websocket.

framework.controllers;
readonly
return Object;

This property store all registered controllers.

framework.databases;Default: {}
return Object;

This property contains all opened NoSQL embedded database.

framework.errors;Default: []
readonly
return Array;

This property store the last 50 errors.

framework.fs;
readonly

Framework file system for dynamic manipulating web application files.

framework.global;Default: {}
return Object;

A global property for storing different values.

framework.helpers;Default: {}
return Object;

This property store all helpers for rendering views. Views support only function, that means - you must store only functions.

framework.ip;Default: localhost
readonly
return String;

Property return IP address on the server listen.

framework.path;
readonly

Framework path for getting all framework paths.

framework.port;
readonly
return Number;

Property return PORT number on the server listen.

framework.resources;Default: {}
internal
return Object;

This property store all content from resource files. This property is cleaned every 20 minutes.

framework.restrictions;
readonly

This property serve for set request restrictions.

framework.server;
readonly
return Object;

Property return HTTP or HTTPS object.

framework.stats;

Framework internal request/response stats.

framework.temporary;
internal
return Object;

This property store internal information about paths and streaming static files. This property is cleaned every 20 minutes.

framework.version;
readonly
return Number;

Return the framework version.

framework.versionNode;
readonly
return Number;

Return the node.js version.

framework.versions;Default: {}
return Object;

This property contains static files mapping.

framework.wokers;Default: {}
return Object;

This property contains all alive workers.

Methods


framework.assert(name, url, callback, [method], [data], [headers], [xhr])framework.clear()framework.configure(value, [rewrite])framework.database(name)framework.decrypt(name, key, [jsonConvert])framework.encrypt(value, key, [isUnique])framework.error(err, name, [uri])framework.eval(script)framework.file(name, [fnValid], fnExec)framework.hash(type, value, [salt])framework.injectConfig(url, [debug], [rewrite])framework.injectController(name, url)framework.injectDefinition(url)framework.injectModule(name, url)framework.injectVersions(url, [rewrite])framework.isProcessed(filename)framework.log([param..1], [param..2], [param..n])framework.module(name)framework.notModified(req, res, [compare], [strict])framework.partial([name], fn)framework.reconnect()framework.redirect(oldHost, newHost, [copyPath], [permanent])framework.refresh([clear])framework.resource([name], key)framework.response401(req, res)framework.response403(req, res)framework.response404(req, res)framework.response500(req, res, err)framework.response501(req, res)framework.responseContent(req, res, code, contentBody, contentType, [compress], [heades])framework.responseCustom(req, res)framework.responseFile(req, res, filename, [downloadName], [headers])framework.responseImage(req, res, filename, fnProcess, [headers], [useImageMagick])framework.responseImageWithoutCache(req, res, filename, fnProcess, [headers], [useImageMagick])framework.responsePipe(req, res, url, [headers], [timeout], [callback])framework.responseRedirect(req, res, url, [permament])framework.responseStatic(req, res)framework.responseStream(req, res, contentType, stream, [downloadName], [headers])framework.route(url, fn, [options])framework.routeCSS(name)framework.routeDownload(name)framework.routeFont(name)framework.routeImage(name)framework.routeJS(name)framework.routeStatic(name)framework.routeVideo(name)framework.run(http, debug, [port], [ip], [options])framework.setModified(req, res, value)framework.stop([code])framework.test(stop, [names], [callback])framework.unlink(list, [callback])framework.usage([detailed])framework.websocket(url, fn)framework.worker(name, [id], [timeout])
framework.assert(name, url, callback, [method], [data], [headers], [xhr]);
return Framework;

Check if ETag or Last Modified has modified. If @compare === {String} compare if-none-match .... if @compare === {Date} compare if-modified-since. This function automatically flush a response 304 (if not modified).

framework.clear();
return Framework;

Clear temporary directory - remove all files in config['directory-temp'].

framework.configure(value, [rewrite]);
return Framework;

Configure framework.

framework.database(name);

Get a NoSQL embedded database connection.

framework.decrypt(name, key, [jsonConvert]);
return String or Object;

Internal cryptography function for decrypting of string or object.

framework.encrypt(value, key, [isUnique]);
return String;

Internal cryptography function for encrypting of string or object. As sugar is config['secret'].

framework.error(err, name, [uri]);
return Framework;

Create a new framework error.

framework.eval(script);
return Framework;

Eval script (live scripting).

framework.file(name, [fnValid], fnExec);
return Framework;

Register a new file route to controller or module. This function can handle static files in a controller. This function must be executed in a controller/module file. For more information look up to the example.

framework.hash(type, value, [salt]);
return String;

Hashing strings.

framework.injectConfig(url, [debug], [rewrite]);
return Framework;

Inject the configuration from URL address. configuration must be in partial.js config format.

framework.injectController(name, url);
return Framework;

Inject a controller from URL address. Controller must be a 100% pure javascript file.

framework.injectDefinition(url);
return Framework;

Inject a definitions from URL address. Definition must be a 100% pure javascript file.

framework.injectModule(name, url);
return Framework;

Inject a module from URL address. Module must be a 100% pure javascript file.

framework.injectVersions(url, [rewrite]);
return Framework;

Inject a versions from URL address.

framework.isProcessed(filename);
internal
return Boolean;

Indicates whether the static file is processed internally. This function is bounded with framework.responseFile() or framework.responseImage().

framework.log([param..1], [param..2], [param..n]);
return Framework;

Write to the log file.

framework.module(name);
return Object;

Get a framework module. All modules are defined in config['directory-modules'].

framework.notModified(req, res, [compare], [strict]);
return Boolean;

Check if ETag or Last Modified has modified. If @compare === {String} compare if-none-match .... if @compare === {Date} compare if-modified-since. This function automatically flush a response 304 (if not modified). If content is not modified then return true.

framework.partial([name], fn);
return Framework;

Register a partial content. Partial content is executed before executing Controller / Module. Private partial content (specified [name]) you must map into the route and global partial content is executed in each request to the controller. For more information look up to the example.

framework.reconnect();
return Framework;

Reconnect HTTP/HTTPS.

framework.redirect(oldHost, newHost, [copyPath], [permanent]);
return Framework;

Register route for HOST redirecting. This feature you can define in definitions.

framework.refresh([clear]);
return Framework;

Refresh internal information (re-configure) and clear temporary data, resources, etc..

framework.resource([name], key);
return String;

Read value from resource file.

framework.response401(req, res);
return Framework;

Response 401 - Unauthorized.

framework.response403(req, res);
return Framework;

Response 403 - Forbidden.

framework.response404(req, res);
return Framework;

Response 404 - Not Found.

framework.response500(req, res, err);
return Framework;

Response 500 - Internal server error.

framework.response501(req, res);
return Framework;

Response 501 - Not Implemented.

framework.responseContent(req, res, code, contentBody, contentType, [compress], [heades]);
return Framework;

Response custom content.

framework.responseCustom(req, res);
internal
return Framework;

Custom response. This function clears uploaded files, creates request/response stats and emits event - request-end.

framework.responseFile(req, res, filename, [downloadName], [headers]);
return Framework;

Response file. A file extension must exists in config['static-accepts']. This function sets up HTTP cache (by the framework config mode - debug/release). If file is CSS or JS then will compiled. This function automatically supports HTTP RANGE streaming.

framework.responseImage(req, res, filename, fnProcess, [headers], [useImageMagick]);
return Framework;

Response image. A file extension must exists in config['static-accepts']. This function sets up HTTP cache (accoring the framework config mode - debug/release) and processed image is saved into the temporary directory.

framework.responseImageWithoutCache(req, res, filename, fnProcess, [headers], [useImageMagick]);
return Framework;

Response image without cache (slowly than responseImage()). A file extension must exists in config['static-accepts'].

framework.responsePipe(req, res, url, [headers], [timeout], [callback]);
importanthttp
return FrameworkController;

Create response from URL (copy response to a response without headers).

framework.responseRedirect(req, res, url, [permament]);
return Framework;

Response redirect

framework.responseStatic(req, res);
internal
return Framework;

Response static file by the request path.

framework.responseStream(req, res, contentType, stream, [downloadName], [headers]);
return Framework;

Response stream. All text/* content-types are compressed via GZIP (if is enabled config['allow-gzip']).

framework.route(url, fn, [options]);
return Framework;

Register a new HTTP/HTTPS route. This function must be executed in a controller/module file.

framework.routeCSS(name);
return String;

Route a CSS file accoring the config['static-url-css']. Returns the relative URL address.

framework.routeDownload(name);
return String;

Route to download directory according the config['static-url-download']. Returns the relative URL address.

framework.routeFont(name);
return String;

Route a font file accoring the config['static-url-font']. Returns the relative URL address.

framework.routeImage(name);
return String;

Route a image file accoring the config['static-url-image']. Returns the relative URL address.

framework.routeJS(name);
return String;

Route a JS file accoring the config['static-url-js']. Returns the relative URL address.

framework.routeStatic(name);
return String;

Route a static file accoring the config['static-url']. Returns relative URL address.

framework.routeVideo(name);
return String;

Route a video file accoring the config['static-url-video']. Returns the relative URL address.

framework.run(http, debug, [port], [ip], [options]);
important
return Framework;

Run the framework.

framework.setModified(req, res, value);
return Framework;

Set last modified or Etag header.

framework.stop([code]);
return Framework;

Stop the framework

framework.test(stop, [names], [callback]);
return Framework;

Run all tests in config['directory-tests'] (or only specific tests).

return Framework;

Remove files.

framework.usage([detailed]);
return String;

Returned string is the Markdown syntax and contains usage informations. Look at this: http://www.partialjs.com/usage/

framework.websocket(url, fn);
return Framework;

Register a new Websocket route to controller or module. This function must be executed in a controller/module file. For more information look up to the example.

framework.worker(name, [id], [timeout]);
return ChildProcess;

Create worker.

Delegates


framework.onAuthorization(req, res, flags, callback)framework.onCompileCSS(filename, content)framework.onCompileJS(filename, content)framework.onError(err, name, uri)framework.onLoad(framework)framework.onMeta(param1, param2, param...n)framework.onPrefix(req)framework.onRequest(req, res)framework.onRoute(req, res)framework.onSettings(param1, param2, param...n)framework.onValidation(name, value)framework.onVersion(name)
framework.onAuthorization(req, res, flags, callback);Default: null
must call a callback

Authorization delegation for authenticating users. If user is logged then value in the callback must be a true or if is unlogged then must be a false. If is user authorized then is added 'logged' flag to the request flags otherwise 'unlogged'. If you forget call callback then is executed timeout.

framework.onCompileCSS(filename, content);Default: null
must return a String value
return String;

This delegate replace an internal CSS compiler. For more information look up to the example. Delegate is not async.

framework.onCompileJS(filename, content);Default: null
must return a String value
return String;

This delegate replace an internal JS compiler. For more information look up to the example. Delegate is not async.

framework.onError(err, name, uri);Default: console.log(err.toString(), err.stack);

If framework throw error then is execute this delegate.

framework.onLoad(framework);Default: null

This delegate is execute when is framework loaded.

framework.onMeta(param1, param2, param...n);
must return a String value
return String;

Render meta tags into the view.

framework.onPrefix(req);Default: null
must return a String value
return String;

This delegate setup prefix for a request. By the prefix you can return view with the prefix without change code in the controller. IMPORTANT: you must return {String}. Empty string or null value show a view without prefix.

framework.onRequest(req, res);Default: null
must return a Boolean value
return Boolean;

Pre-request delegate. This delegate is executed first.

framework.onRoute(req, res);Default: null
must return a Boolean value
return Boolean;

Refusing request. You must return Boolean value. If false then is request refused.

framework.onSettings(param1, param2, param...n);Default: null
must return a String value
return String;

Render custom settings into the view.

framework.onValidation(name, value);Default: null
return Boolean;

Validation delegate for validating data models. You must return Boolean. If true then is value validated.

framework.onVersion(name);Default: null
must return a String value
return String;

This delegate adjusted file name of static file. Into the delegate are inserted all static files (e.g.: JavaScript, CSS, images from IMG tags, images from CSS, etc.). IMPORTANT: you must return {String} - new file name. For understand delegate click on the example.

Events


framework.on('clear', name, [value])framework.on('configure', config)framework.on('controller', controller, name)framework.on('exit')framework.on('expire', name, value)framework.on('load', framework)framework.on('ready', count)framework.on('reconfigure')framework.on('request-begin', req, res)framework.on('request-end', req, res)framework.on('service', count)framework.on('websocket-begin', controller, client)framework.on('websocket-end', controller, client)
framework.on('clear', function(name, [value]) {    // YOUR CODE HERE});

This event is triggered when framework clears internal information.

framework.on('configure', function(config) {    // YOUR CODE HERE});

This event is triggered when is re-configured the framework.

framework.on('controller', function(controller, name) {    // YOUR CODE HERE});

This event is called every Request to Controller. In this event you can cancel request to the controller through controller.cancel().

framework.on('exit', function() {    // YOUR CODE HERE});

This event is triggered when framework is stopped. This event will not trigger when application stopped by force.

framework.on('expire', function(name, value) {    // YOUR CODE HERE});

This event is triggered when expired item in the cache. This item was removed from a cache.

framework.on('load', function(framework) {    // YOUR CODE HERE});
onetime

This event is triggered after is framework ready and after are loaded Definitions, Controllers, Modules, etc..

framework.on('ready', function(count) {    // YOUR CODE HERE});
onetime

This event is triggered after is framework ready and after are loaded Definitions, Controllers, Modules, etc..

framework.on('reconfigure', function() {    // YOUR CODE HERE});

This event is triggered when the framework refreshed a configuration file and mapping versions.

framework.on('request-begin', function(req, res) {    // YOUR CODE HERE});

This event is triggered when begin HTTP request to a controller (NOT VALID FOR STATIC FILES).

framework.on('request-end', function(req, res) {    // YOUR CODE HERE});

This event is triggered when end HTTP request from a controller (NOT VALID FOR STATIC FILES).

framework.on('service', function(count) {    // YOUR CODE HERE});
every 60 seconds

This event is triggered every 60 seconds and is used for cleaning temporary data / cache.

framework.on('websocket-begin', function(controller, client) {    // YOUR CODE HERE});

This event is triggered when begin Websocket request to a controller.

framework.on('websocket-end', function(controller, client) {    // YOUR CODE HERE});

This event is triggered when end WebSocket request from a controller.