Skip to content

Examples Applets

Michael Anderson edited this page Dec 1, 2017 · 1 revision

Applets

An applet is an Express app that is mounted at a specific location on the server.

Setup

Applets inherit from the coffeecat-applet class. Install the base applet with npm:

npm install -s git+https://github.com/mlaanderson/coffeecat-applet.git

Static Page App

A basic applet can just serve static pages:

const path = require('path');
const Applet = require('coffeecat-applet');
var express = require('express');

class MyApplet extends Applet {
    constructor(config) {
        super(config);

        process.nextTick((() => {
            this.app.use(express.static(path.join(__dirname, 'public')));
        }).bind(this));
    }
}

module.exports = MyApplet;

Simple App

const path = require('path');
const Applet = require('coffeecat-applet');

var express = require('express');

class MyApplet extends Applet {
    constructor(config, webSocketServer) {
        super(config, webSocketServer);

        process.nextTick((() => {
            // set the view engine and template location
            this.setViewEngine('ejs');
            this.setViewPath(path.join(__dirname, 'views'));

            // configure the session
            this.setSession('express-session', {
                secret: '95f0d5bd-b3aa-482d-8469-b6ee04776d8a',
                resave: false,
                saveUninitialized: true,
                cookie: { 
                    secure: true,
                    path: this.configuration.applet.container
                }
            });

            // configure the static file directory
            this.app.use(express.static(path.join(__dirname, 'public')));

            this.app.get('/', (req, res, next) => {
                // Perform some processing
                req.session.count = req.session.count || 0;
                req.session.count++;
                
                // Collect data to present
                var data = { count: req.session.count };

                // Render the view
                res.render('index', data);
            });
        }).bind(this));
    }
}

module.exports = MyApplet;

Clone this wiki locally