0.8.5 • Published 4 years ago

metaserve v0.8.5

Weekly downloads
178
License
-
Repository
github
Last release
4 years ago

metaserve

metaserve makes web application prototyping quicker by compiling and serving assets built with meta-languages[1] such as CoffeeScript, Jade, and Styl (currently the full list).

Use as a command or as middleware to handle requests for e.g. js/myapp.js by run-time-compiling the js/myapp.coffee source file into Javascript. Similar to (but less contrived than) the Rails Asset Pipeline.

Metaserve is based on a collection of plugins, which by default support Jade (to HTML), CoffeeScript (to Javascript), and Sass (to CSS) to support the Prontotype stack. New languages are easily added with a simple plugin architecture.

As a command

Install with npm install -g metaserve

Use within a directory that has a bunch of .jade, .sass and .coffee. Run metaserve with optional arguments --host and --port. Defaults to 0.0.0.0:8000.

As middleware

Install with npm install metaserve

Use by supplying a base directory, then hooking it in as Express/Connect middleware...

var express = require('express');
var metaserve = require('metaserve');

app = express();
app.use(metaserve('./app'));
app.listen(8550);

... or as a fallback method in a standard http server:

var http = require('http');
var metaserve = require('metaserve')('./app');

var server = http.createServer(function(req, res) {
    if (req.url === '/dogs') {
        return res.end('woof');
    } else {
        return metaserve(req, res);
    }
});

server.listen(8550);

Writing Plugins

A plugin is simply a Javascript module with a few fields:

  • ext
    • The extension that this plugin will match, e.g. "coffee"
  • default_config (optional)
    • Default config which will be extended and passed in as the config argument to the compiler function
  • compiler(filename, config, context, cb)
    • A function that should transform some source file and call back with an object {content_type, compiled}

Here's a simple plugin that reads and reverses a text file:

var fs = require('fs');

module.exports = {
    ext: 'txt',

    compile: function(filename, config, context, cb) {
        fs.readFile(filename, function(err, source) {
            if (err)
                return cb(err);
            else {
                source = source.toString();
                reversed = source.split('').reverse().join('')
                cb(null, {
                    content_type: 'text/plain',
                    compiled: reversed
                });
            }
        });
    }
};

By passing an object of compilers to the metaserve middleware, paths that match the extension key (here "txt") will be run through this plugin:

app.use(metaserve('./app', {
    txt: require('./reverse-plugin')
}));

Default Plugins


Notes

  1. Not to be confused with the real definition of a Metalanguage.
0.8.5

4 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.3

6 years ago

0.7.2

6 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.4.7

7 years ago

0.4.6

7 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.7

8 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago