Giter Site home page Giter Site logo

rill-js / rill Goto Github PK

View Code? Open in Web Editor NEW
612.0 16.0 21.0 795 KB

🗺 Universal router for web applications.

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

License: MIT License

JavaScript 0.67% Makefile 0.32% Shell 0.37% TypeScript 98.64%
nodejs middleware router isomorphic universal

rill's Introduction

Expressive router for nodejs 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 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, React, Svelte and even html only template engines such as Pug.

Installation

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 for a good polyfill, babel-polyfill also covers this.

Community

Articles

Why Rill?

Rill is the answer to a simple question; Can I run my 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. Many isomorphic frameworks and routers have crazy abstractions and learning curves but with Rill, if you understand Express or 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 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 you will quickly notice there is ZERO browser specific code. This is all thanks to @rill/http which is node's HTTP.createServer 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 for each of these events and emit it through the router, similar to how receiving a request works in nodejs.

It supports everything you'd expect from a client side nodejs server. This includes redirects, refreshes, cookies, scrolling and url updates using the History API.

Example

Create an app

/**
 * 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

// 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

// 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 = (
    <html>
      <head>
        <title>My App</title>
        <meta name="description" content="Rill Application">
      </head>
      <body>
        <form action="/add-todo" method="POST">
          <h1>Just a plain old form</h1>
          <input type="text" name="todo"/>
          <button type="submit">Add Todo</button>
        </form>

        {todolist.length
          ? todolist.map(renderTodo)
          : 'No todos to display.'
        }
        <script src="/app.js"/>
      </body>
    </html>
  )
})

Handle a form submission

// 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

// 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 - A browserify transform to remove server-side code.
  • isomorphic-fetch - Universal http requests using WHATWG fetch.
  • isomorphic-form-data - Send multipart form data universally (able to send files and works with fetch).
  • scroll-behavior - @rill/http will automatically try to use the "smooth" scroll-behavior when scrolling to targets on link clicks. This will polyfill that across modern browsers.
  • submit-form - Manually trigger Rill navigation in the browser.

Prior Art

  • koa-client - Koa clone that runs in the browser, inspired this package.
  • monorouter - Another isomorphic router that partially inspired this package.

Contributions

  • Use npm test to build and run tests.

License

MIT

rill's People

Contributors

dylanpiercey avatar gitter-badger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rill's Issues

Res.status returns 404 when using promises in routes

Hello,

The response object returns a 404 whenever I use promises with the routes. When I remove the promise, the response is resolved normally. The examples on the site are done with async/await, so I think that is likely the issue. Is there any example using regular promises?

Cleanup

  • rename ctx,request to ctx.req and ctx,response to ctx.res.
  • make ctx.req.original and ctx.res.original.
  • Remove cookies from context and add a getter on req and a setter on res.
  • Write compression middleware for non static assets.

More examples?

This looks interesting. When would there be more examples?

Props

Not react props. I just wanted to give you some props for making this. I've been thinking about how great it would be if this existed and attempted once. But got too tricky for me trying to extrapolate express routes for the browser. I'll be following closely!

Funny story as well, I put something together last week, less of a framework but something that puts together webpack and hmr together for ssr. Not complete but works given the right conditions right now. Need to add in configurability. checkout, http://github.com/uptownhr/kube.

Right now, i'm trying to see how I can use rill, instead of express and see where that can go.

Grouping routes for similar functions into seperate files

Hello,

Is it possible to group routes into seperate files the same way you would do it in express?

In express, you could just do:

app.use('/animals', require('./animals'))

and inside animal.js, you could have:

var express = require('express')
  , router = express.Router()

// Domestic animals page
router.get('/domestic', function(req, res) {
  res.send('Cow, Horse, Sheep')
})

// Wild animals page
router.get('/wild', function(req, res) {
  res.send('Wolf, Fox, Eagle')
})

module.exports = router

Such that all the routes are preceded with /animals. Can this be achieved in rill as well?

Documentation: Transpiling JSX / React

The main example on the readme would not work without a step that transpiles JSX / React.

I'll submit a PR as soon as (and if) I figure out an easy way to do that.

PS: Thanks, the concept of this framework is quite exciting. I'll be playing with it quite a bit over my weekends. 👍

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.