Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/rill-js/rill

🗺 Universal router for web applications.
https://github.com/rill-js/rill

isomorphic middleware nodejs router universal

Last synced: 2 months ago
JSON representation

🗺 Universal router for web applications.

Lists

README

        



Rill Logo




API stability



TypeScript



NPM version



Build status



Test Coverage



Downloads



Browser Bundle Size (Gzipped)



Gitter Chat



Sauce Test Status

Expressive router for [nodejs](https://nodejs.org) and the browser.
Rill brings cascading middleware to the browser and enables a familiar routing solution for web applications.

Rill provides the minimum for abstractions over [nodejs](https://nodejs.org) and the browser enabling things like routing (with redirecting, refreshes and more), cookies, and middleware with the same api.

It supports many view engines including [Marko](http://markojs.com), [React](https://reactjs.org), [Svelte](https://svelte.technology) and even html only template engines such as [Pug](https://pugjs.org).

# Installation

```console
npm install rill
```

# Browser support
All modern browsers are supported including IE10 and above.
Older browsers will need to polyfill the Promise API, checkout [es6-promise](https://github.com/stefanpenner/es6-promise) for a good polyfill, babel-polyfill also covers this.

# Community

* [API Documentation](https://github.com/rill-js/rill/blob/master/docs/api/index.md#introduction)
* Examples
* [React](https://github.com/rill-js/todomvc-react)
* [Marko](https://github.com/rill-js/todomvc-marko)
* [Svelte](https://github.com/rill-js/todomvc-svelte)
* [Wiki](https://github.com/rill-js/rill/wiki)
* [FAQ](https://github.com/rill-js/rill/wiki/FAQ)
* [Middleware List](https://github.com/rill-js/rill/wiki/Middleware)
* [Gitter Community](https://gitter.im/rill-js/rill)
* [Reddit Community](https://www.reddit.com/r/Rill)

# Articles

* [Universal web application framework - Interview with Dylan Piercey](https://survivejs.com/blog/rill-interview/)
* [Isomorphic Javascript, let’s make it easier.](https://medium.com/@pierceydylan/isomorphic-javascript-it-just-has-to-work-b9da5b0c8035)
* [How to make universal JavaScript applications — Part 1](https://medium.com/@pierceydylan/how-to-make-universal-javascript-applications-part-1-90e9032bc471)
* [Browsers, Servers, and APIs](https://medium.com/@iamjohnhenry/browsers-servers-and-apis-2f7b10523f39)
* [Why Everyone is Talking About Isomorphic](https://medium.com/capital-one-developers/why-everyone-is-talking-about-isomorphic-universal-javascript-and-why-it-matters-38c07c87905#.mdd84j28m)
* [Isomorphic JavaScript: The Future of Web Apps](https://medium.com/airbnb-engineering/isomorphic-javascript-the-future-of-web-apps-10882b7a2ebc)

# Why Rill?
Rill is the answer to a simple question; Can I run my [Express](https://github.com/expressjs/express) style router in the browser? Turns out you can and it works awesome.

It brings a common interface to many typical app like features in both the browser and [nodejs](https://nodejs.org). Many isomorphic frameworks and routers have crazy abstractions and learning curves but with Rill, if you understand [Express](https://github.com/expressjs/express) or [Koa](https://github.com/koajs/koa), you already know how the routing works! In Rill you get to program much of your application logic using the same api (client or server) including routing, rendering, data fetching and more are easily shared.

Rill also works perfectly as a stand alone router for [nodejs](https://nodejs.org) or in the browser. This allows for easy progressive enhancement. If all is well the browser can handle much of your application logic and if JavaScript fails for any reason your server knows exactly what to do.

# How does this thing work?
If you look at the source for Rill [here](https://github.com/rill-js/rill/tree/master/src) you will quickly notice there is ZERO browser specific code. This is all thanks to [@rill/http](https://github.com/rill-js/http) which is node's [HTTP.createServer](https://nodejs.org/api/http.html#http_http_createserver_requestlistener) ported to the browser.

In the browser it works by listening for internal link clicks, form submissions and browser history changes. It will then create a [Rill Context](https://github.com/rill-js/rill/blob/master/docs/api/context.md#context) for each of these events and emit it through the router, similar to how receiving a request works in [nodejs](https://nodejs.org).

It supports everything you'd expect from a client side [nodejs](https://nodejs.org) server. This includes redirects, refreshes, cookies, scrolling and url updates using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History).

# Example

### Create an app

```javascript
/**
* The following code can run 100% in the browser or in nodejs.
* Examples use es2015/2016 with Babel and JSX but this is optional.
*/

import Rill from 'rill'
const app = new Rill() // You can call Rill without new, but autocomplete will not work.
```

### Setup middleware

```javascript
// Universal form data parsing middleware.
import bodyParser from '@rill/body'
app.use(bodyParser())

// Universal react rendering middleware.
import reactRenderer from '@rill/react'
app.use(reactRenderer())

// Example Logger
app.use(async ({ req }, next)=> {
const start = Date.now()

// Rill uses promises for control flow.
// ES2016 async functions work great as well!
await next()

const ms = Date.now() - start
console.log(`${req.method} ${req.url} - ${ms}`)
})
```

### Setup a page

```javascript
// Respond to a GET request.
app.get('/todos', async ({ res })=> {
// Fetch a todolist from some service.
const todolist = await MyTodoListService.getAllTodos()

// Directly set React virtual dom to the body thanks to @rill/react.
// (Checkout @rill/html for universal html diffing).
res.body = (


My App




Just a plain old form



Add Todo

{todolist.length
? todolist.map(renderTodo)
: 'No todos to display.'
}



)
})
```

### Handle a form submission
```javascript
// Respond to a POST request.
app.post('/add-todo', async ({ req, res })=> {
// We handle form submissions with Rill the same way one would express or koa.
// Here we are simply adding the todo via some service.
await MyTodoListService.addTodo({ text: req.body.todo })
// And then we redirect back (same as res.redirect('/todos'))
res.redirect('back')
})
```

### Start app

```javascript
// Start a regular http server.
// In the browser any form submissions or link clicks will intercepted by @rill/http.
app.listen({ port: 80 })
```

## See Also

* [isbrowser](https://github.com/DylanPiercey/isbrowser) - A browserify transform to remove server-side code.
* [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) - Universal http requests using WHATWG fetch.
* [isomorphic-form-data](https://github.com/form-data/isomorphic-form-data) - Send multipart form data universally (able to send files and works with fetch).
* [scroll-behavior](https://github.com/DylanPiercey/scroll-behaviour) - @rill/http will automatically try to use the "smooth" [scroll-behavior](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior) when scrolling to targets on link clicks. This will polyfill that across modern browsers.
* [submit-form](https://github.com/DylanPiercey/submit-form) - Manually trigger Rill navigation in the browser.

## Prior Art

* [koa-client](https://github.com/kentjs/koa-client) - Koa clone that runs in the browser, inspired this package.
* [monorouter](https://github.com/matthewwithanm/monorouter) - Another isomorphic router that partially inspired this package.

## Contributions

* Use `npm test` to build and run tests.

## License

[MIT](https://tldrlegal.com/license/mit-license)