[go: up one dir, main page]

AppJet Introduction

An AppJet app is a server-side JavaScript program that runs when a request is made to the app's domain (app-name.appjet.com).

Though AppJet is a server-side environment, the "core" JavaScript language is the same as you may be familiar with from the browser -- the same strings, objects, arrays, dates, and so on.

Beyond the core language is our set of libraries, which make it easy to build a simple web app.

Here is a complete AppJet app, a "shoutbox" where anyone can type in a message, and the most recent messages are displayed. It shows off persistent storage, responding to form input, and displaying HTML. Visit it at http://shoutbox.appjet.net/.

import("storage");

var historySize = 20;

if (! storage.msgs) {
    storage.msgs = new StorableObject();
    storage.start = 0; // first msg
    storage.end = 0; // index after last msg
}

if (request.isPost) {
    var newText = request.params.newText;
    if (acceptable(newText)) {
        storage.msgs[storage.end++] = request.params.newText;
        while (storage.end - storage.start > historySize) {
            delete storage.msgs[storage.start++];
        }
    }
    response.redirect("/");
} else {
    print(FORM({id:"shoutform",action:"/", method:"post"},
          INPUT({type:"text", name:"newText", size:40}),
          INPUT({type:"submit", value:"shout!"})));
    var messageDiv = DIV({id:"msgs"});
    for(var i=storage.end-1; i >= storage.start; i--) {
        messageDiv.push(P(storage.msgs[i]));
    }
    print(messageDiv);
}

// TEMP: anti-spam tactics added to block annoying techcrunch spam.
// We limit each IP address's posts.
function acceptable(txt) {
    if (!storage.ips) { storage.ips = new StorableObject(); }
    if (!storage.ips[request.clientAddr]) {
        storage.ips[request.clientAddr] = 0;
    }
    storage.ips[request.clientAddr]++;
    return (storage.ips[request.clientAddr] <= 40 ||
            request.clientAddr == storage.ajtc);
}

/* appjet:css */

#shoutform { margin: 10px; }
#msgs { height: 200px; overflow: auto; border: 1px solid #666;
  margin: 10px; padding-left: 10px; }
#msgs p { margin-top: 0.6em; margin-bottom: 0.6em; }
body { margin: 8px; padding: 0; margin-top: 6px; }
© Copyright 2007-2008 AppJet Inc.