GithubHelp home page GithubHelp logo

mach's Introduction

Mach

Mach is a better way to build fully asynchronous web servers using node.js. It has the following goals:

  • Simplicity: straightforward mapping of HTTP requests to JavaScript function calls
  • Asynchronous: responses can be deferred using a standard Promises/A+ compatible promise
  • Streaming: request and response bodies can be streamed
  • Composability: middleware composes easily using promises
  • Robust: Promises propagate errors up the call stack, simplifying error handling

"Hello world" in Mach is simple.

require("mach").serve(function (request) {
  return "Hello world!";
});

More complex applications can be built through the use of various middleware. Mach includes middleware for doing many common tasks including Sinatra-style request routing, HTTP caching, content negotiation, multipart handling, and much more. Asynchronous responses are fully supported through the use of promises.

A more full-featured example might look like this:

var mach = require('mach');
var app = mach.stack();

// Log all requests.
app.use(mach.logger);

// Serve requests to the "files" subdomain with a static file server.
app.map('files.example.com', function (app) {
  app.use(mach.file, '/www/downloads');
});

app.use(mach.session);    // HTTP sessions
app.use(mach.params);     // Parse request parameters

app.get('/', function (request) {
  return "Hello world!";
});

// GET /posts/123.json
app.get('/posts/:post_id.json', function (request, postId) {
  return query('SELECT * FROM posts WHERE id=?', postId).then(JSON.stringify);
});

// POST /posts/123/comments
app.post('/posts/:post_id/comments', function (request) {
  var authorId = request.params.author_id;
  var commentBody = request.params.body;
  // ...
  return 201;
});

// Serve the app, listening on port 3000.
mach.serve(app, 3000);

Please checkout the docs for more information and lots of usage examples.

Issues

Please file issues on the issue tracker on GitHub.

Specs

Before you run the specs, do an npm install.

To run all the specs:

$ mocha spec

To run an individual spec:

$ mocha spec/content-type-spec.js

The Redis session store specs rely on Redis to run successfully. By default they are skipped, but if you want to run them fire up a Redis server on the default host and port and set the $WITH_REDIS environment variable.

$ WITH_REDIS=1 mocha spec

Influences

mach's People

Contributors

maccman avatar mjackson avatar tlrobinson avatar

Watchers

 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.