Giter Site home page Giter Site logo

snapp's Introduction

Snapp

Or, Stop Manually Writing Lists of Routes and Start Writing your App!

Snapp makes automatic routing of your application insanely easy. It must be magic!

Installation

Not yet available in npm. For now, manually clone this repository under your node-modules folder.

Basic Example App

This makes a very simple, but fully operation webapp! Save the following as example.js:

var app = require('snapp');

app.hello = 'Hello World';

app.$index = {
  $status: 200,
  $mime: 'text/html',
  $content: '<img src="/logo.png" /><a href="/hello">Say Hi</a>'
};

app['logo.png'] = app.$file('logo.png');

To start, you must specify a port to run on with the -p option, as such:

node example.js -p 8080

To see the logo, add a logo.png in the same directory as your app.

Advanced Example: URL Math

Just for fun! See if you understand this, and how to properly visit it in the browser :) Pay special attention to which URLs generate a 404 error, and which URLs work.

var app = require('snapp'),
    f = parseFloat;

app.math = function(callback, context) {
    var num1 = f(context.arg());
    callback({
        add: function(callback, context) {
            callback(num1 + f(context.arg()));
        },
        multiply:function(callback, context) {
            callback(num1 * f(context.arg()));
        }
    });
};

Custom Error Pages

Normally, a 500 error shows the stack trace in the browser. If you would like to change this behavior, just add a $500 page to your application. This example logs the error to the console.

app.$500 = function(callback, context) {
    console.log(context.error.stack);
    callback('Server Error');
};

To be Continued...

snapp's People

Contributors

nateferrero avatar

Stargazers

Gun.io Robot avatar

Watchers

Gun.io Robot avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.