Home

Awesome

micro-app-framework

Simple framework in Node,js for building small one page apps when you don't need/want a more sophisticated framework.

I had built a number of small apps that I keep ruinning on my desktop and extracted the common framework so that it could be more easily used for future apps. Typically the apps have a small single page GUI.

The framework makes it easy to create the app where you only need to focus on the app functionality and page layout while the framework can handle the common requirements like the http server, tls support, authentication etc.

The framework handles opening a pop-up page with the appropriate size of the application GUI page. If you configure the browser to allow javascript to close windows it will also close the window/page from which the application is launched.

New You can now get native look and feel for micro-apps without changing the app itself. It also removes the browser address bar making the application window more compact. See the project [micro-app-electron-launcher] (https://github.com/mhdawson/micro-app-electron-launcher) for details.

Examples

Examples of existing apps using the framework are shown in the following sections.

These existing micro-apps are a good starting point when you want to create your first application based on the framework.

micro-app-timezones

Simple micro-app to keep track of the current time for people you work with.

micr-app-timezones

sample timezones page

micro-app-phone-dialer

Simple phone dialer for cisco phones.

micro-app-phone-dialer

sample phone-dialer page

micro-app-simple-dashboard

Micro app to display a home dashboard showing temperature, power usage and other data collected from local sensors. The dashboard is updated in realtime using socket.io.

micro-app-simple-dashboard

picture of dashboard main window

Usage

To build a micro-app using the framework you need to do the following:

Server.js

Server.js must define an object called "Sever" which must support the following methods:

The applicaiton can define its own substitutions in page.html.template but in addition the framework uses/provides the following by default:

In order to allow the micro-app to be started on its own (as opposed to being started by the framework which will be supported in later versions) the following should be at the end of server.js

<PRE> if (require.main === module) { var path = require('path'); var microAppFramework = require('micro-app-framework'); microAppFramework(path.join(__dirname), Server); } module.exports = Server; </PRE>

page.html.template

page.html.template provides the main page with the GUI for the applications.

It should use <TITLE> as the title so that uses the value from the configuration files.

Other values can be used as required by the html/javascript defined for the application.

For addtional connections (ex using socket.io) it should use <URL_TYPE> for the connection type and something allong these lines for the connection back to the application:

<PRE> '&lt;URL_TYPE&lt;://' + window.location.host </PRE>

config.json

config.json acts as the configuration file for the application. It can include application specific values which server.js can read by accessing the values in Server.config.

The following are supported by the framework itself:

The application can add any additional configuration values that will be accessible through Server.config. In addition, if authentication is enabled the value can be encrypted. In this case, the value can be decrypted using Server.decryptConfigValue(value). The function decryptConfigValue() will be added to the Server object after authentication is complete and can be used in getTemplateReplacements().

If required the key/certificate can be created using a command along these lines:

<PRE> openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout key.pem -out cert.pem </PRE>

The script lib/gen_password.js can be used to generate the hashed password for authInfo as follows:

<PRE> node lib/gen_passwword.js mypassword </PRE>

The script lib/enc_config_value.js can be used to encrypt a configuration value as follows:

<PRE> node lib/enc_config_value.js value mypassword </PRE>

where value is the configuration value to be encrypted and mypassword must be the same password configured for basic authentication.

The following are are also supported by the framework and useful for debugging:

package.json

package.json whould be as required for your application but it should include the dependency on the lastest version of the micro-app-framework. For example:

<PRE> "dependencies": { "micro-app-framework": "^0.1.1", }, </PRE>

and should include the following to support starting the micro-app with npm:

<PRE> "scripts": { "start": "node lib/server.js", "test": "echo \"Error: no test specified\" && exit 1" }, </PRE>

File layout

The framework expects the files to be layed out as follows:

<PRE> package.json lib/server.js lib/page.html.template lib/config.json lib/key.pem (only needed if tls is enabled) lib/cert.pem (only needed if tls is enabled) </PRE>

and expects that the application will be started using:

<PRE> npm start </PRE>

in the root directory for the application (the directory with package.json)

TODO