All Projects → vanwagonet → middle-router

vanwagonet / middle-router

Licence: MIT license
Route urls on both client and server through middleware

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to middle-router

tulingx
TULINGX(图灵)VPN下载页 翻墙 代理 科学上网 外网 加速器 梯子 路由
Stars: ✭ 59 (+78.79%)
Mutual labels:  router
react-chicane
A simple and safe router for React and TypeScript.
Stars: ✭ 191 (+478.79%)
Mutual labels:  router
use-react-router-breadcrumbs
tiny, flexible, hook for rendering route breadcrumbs with react-router v6
Stars: ✭ 170 (+415.15%)
Mutual labels:  router
es6-router
🌐 Simple client side router built in ES6
Stars: ✭ 16 (-51.52%)
Mutual labels:  router
fast-route
PSR-15 middleware to use FastRoute
Stars: ✭ 91 (+175.76%)
Mutual labels:  router
Parrot
Web router specially designed for building SPAs using Meteor
Stars: ✭ 75 (+127.27%)
Mutual labels:  router
dilovel
An advanced framework is written in PHP, a framework containing rich components such as middleware, orm, request management, template engine, elasticsearch, template engine, many modern frameworks have been written by adopting clean code principles completely written in accordance with PHP standards. like linux operating system ...All of control…
Stars: ✭ 38 (+15.15%)
Mutual labels:  router
http-multiserver.cr
Mount multiple web applications 🚦
Stars: ✭ 23 (-30.3%)
Mutual labels:  router
routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (+15.15%)
Mutual labels:  router
orion
A Crystal router
Stars: ✭ 115 (+248.48%)
Mutual labels:  router
RoundedLayout
This is a library that has a rounded cut of View, support whether the specified corners are cropped and add a custom Border, and add a shadow support from API 9, which is based on FrameLayout, that is, His Child can be any View, the current library or preview version, if you encounter problems in the process can submit issue or pr.
Stars: ✭ 24 (-27.27%)
Mutual labels:  router
journey
A conductor routing helper library
Stars: ✭ 35 (+6.06%)
Mutual labels:  router
Stime
基于Vue-cli3(Vue+Vue-router)构建的单页单栏Typecho主题,全站Ajax+类Pjax(Vue-router)无刷新,自适应适配移动设备
Stars: ✭ 29 (-12.12%)
Mutual labels:  router
lura
Ultra performant API Gateway with middlewares. A project hosted at The Linux Foundation
Stars: ✭ 5,159 (+15533.33%)
Mutual labels:  router
neteng-roadmap
Network Engineering at Scale Roadmap/Landscape
Stars: ✭ 53 (+60.61%)
Mutual labels:  router
svelte-starter-kit
Svelte starter kit — router, state management and testing included.
Stars: ✭ 16 (-51.52%)
Mutual labels:  router
qlevar router
Manage you project Routes. Create nested routes. Simply navigation without context to your pages. Change only one sub widget in your page when navigating to new route.
Stars: ✭ 51 (+54.55%)
Mutual labels:  router
use-router-machine
Router state-machine hook, powered by XState (DEPRECATED).
Stars: ✭ 11 (-66.67%)
Mutual labels:  router
RouteNow
RouteNow is a small fast library ⚡ that will help you in developing a SinglePage Application without any dependencies like jQuery, AngularJs, vue.js or any of those bulky frameworks.
Stars: ✭ 17 (-48.48%)
Mutual labels:  router
Android-XRouter
This is a lightweight and simple routing framework that provides jump routing and method routing.
Stars: ✭ 19 (-42.42%)
Mutual labels:  router

middle-router

npm Version Greenkeeper badge Build Status

MIT License JS Standard Style

Route urls through middleware functions on both client and server.

middle-router is a universal front-end router for routing urls changes through a series of async middleware functions. This allows you to perform multiple tasks when the url changes, instead of just updating the view.

middle-router uses path-to-regexp to match paths, so it should behave much like express 4.x paths. It also uses middle-run to run the middleware series.

Usage

router.js

import Router from 'middle-router'

export default Router()
  .use(async ({ context, next }) => {
    let start = Date.now()
    await next() // start next middleware, wait for control to return
    context.totalMs = Date.now() - start
  })

  .use('/accounts', Router()
    .use('/users/:userId', async ({ params, resolve, beforeExit, exiting }) => {
      setupListeners()
      beforeExit(event => {
        if (isFormDirty) return 'Are you sure you want to leave?'
      })

      resolve(UserView({ id: params.userId })) // yields control back upstream

      await exiting // only do this after resolve, or it won't resolve until next url change!!
      cleanupListeners()
    })
  )

server-client.js

import router from './router.js'
import { Router } from 'express'
import ReactDOMServer from 'react-dom/server'

export default Router()
  .use(async (req, res, next) {
    try {
      let view = await router.route(req.url, res.locals.state)
      res.send(ReactDOMServer.renderToString(view))
    } catch (err) {
      next(err)
    }
  })

client.js

import router from './router.js'
import ReactDOM from 'react-dom'

router
  .on('route', async (args, routing) => {
    try {
      let view = await routing
      ReactDOM.render(view, document.getElementById('view'))
    } catch (error) {
      ReactDOM.render(<Error error={error}/>, document.getElementById('view'))
    }
  })
  .start()

Note: These usage examples use Express and React, and resolve each url to a React element. middle-router has no dependency on these, and can be used with whatever libraries you like.

API

Full API documentation is in the GitHub Wiki

Async Middleware

middle-router can work with any promised-based async middleware, but it was designed specifically for async functions. Inspired by koa's yield next, middle-router allows you to await next() so you can next() "downstream" and the await for control to flow back "upstream".

License

This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].