react-express-router
React Express Router is a declarative router that wrap Express APIs to use they like a react components.
How to install
yarn add react-express-router react
or
npm i react-express-router react
Usage
import React from 'react';import Express Route compile from 'react-express-router'; const handlerHelloWorld = req res res;; const app = <Express> <Route ="/test" ="GET" = /> </Express>; ;
and visit http://localhost:3000/test to look the hello world!
Documentation
Components
Express
This components is used to wrap all routes/middlewares. And when it's compiled returns an Application instance.
Prop | Description | Value | Default |
---|---|---|---|
path | Root path. (optional) | string \| RegExp \| Array<string \| RegExp> |
undefined |
Example:
const app = <Express> <Route ="/test" ="GET" = /> </Express>; ;
Middleware
With this component you can define a middleware. Also you can wrap routes/middlewares with it.
Prop | Description | Value | Default |
---|---|---|---|
path | Access path. (optional) | string \| RegExp \| Array<string \| RegExp> |
undefined |
handle | Action to execute when the middleware is triggered | function(req, res, next) |
- |
More info: http://expressjs.com/en/guide/writing-middleware.html#writing-middleware-for-use-in-express-apps
Example:
const app = <Express> <Middleware = /> <Route ="/user" ="POST" = /> <Route ="/" ="GET" = /> </Express>;
const app = <Express> <Middleware => <Route ="/dashboard" ="GET" = /> <Route ="/profile" ="GET" = /> </Middleware> <Route ="/" ="GET" = /> </Express>;
const app = <Express> <Middleware ="/account" => <Route ="/apps" ="GET" = />' ' /* you can access using /account/apps */ <Route ="/profile" ="GET" = /> /* you can access using /account/profile */ </Middleware> <Route ="/" ="GET" = /> </Express>;
Route
Probably the most important component. It has the responsibility to catch the current request.
Prop | Description | Value | Default |
---|---|---|---|
path | Access path. | string \| RegExp \| Array<string \| RegExp> |
undefined |
method | The http method. See the full list below. | get \| post \| put |
- |
handle | Action to execute when the route is triggered | function(req, res, next) |
- |
caseSensitive | Enable case sensitivity. (optional) | boolean |
false |
mergeParams | Preserve the req.params values from the parent router.(optional) | boolean |
false |
strict | Enable strict routing. (optional) | boolean |
false |
Full list of methods: http://expressjs.com/en/4x/api.html#app.METHOD More info: http://expressjs.com/en/guide/routing.html
Examples:
const app = <Express> <Route ="/" ="GET" = /> <Route ="/about" ="GET" = /> <Route ="/products"> <Route ="/:id" ="GET" = /> <Route ="/:id" ="POST" = /> <Route ="/:id" ="PUT" = /> <Route ="/" ="GET" = /> </Route> </Express>;
// also you can nested more routesconst app = <Express> <Route ="/" ="GET" = /> <Route ="/about" ="GET" = /> <Route ="/products"> <Route ="/:id" ="GET" = /> <Route ="/:id" ="POST" = /> <Route ="/:id" ="PUT" = /> <Route ="/" ="GET" = /> <Route ="/details"> <Route ="/" ="GET" = /> <Route ="/shippings"> <Route ="/" ="GET" = /> <Route ="/:id" ="GET" = /> </Route> </Route> </Route> </Express>;
ErrorHandler
Component to catch and process an error with the request. Only one per app.
Prop | Description | Value | Default |
---|---|---|---|
handle | Action to execute when the error handler is triggered | function(err, req, res, next) |
- |
More info: http://expressjs.com/en/guide/error-handling.html#error-handling
const app = <Express> <Route ="/" ="GET" = /> <ErrorHandler = /> </Express>;
ParamMiddleware
This middleware is used to handler custom params.
Prop | Description | Value | Default |
---|---|---|---|
name | Name of the param (optional) | string |
undefined |
handle | Action to execute when the error handler is triggered | function(req, res, next, val, name) |
- |
More info: http://expressjs.com/en/api.html#app.param
Custom Components
Also you can create your own components with the native components.
Example:
const UserRouter = <Middleware ="/user" => <Route ="/" ="GET" = /> <Route ="/settings" ="GET" = /> </Middleware>; const app = <Express> <Route ="/" ="GET" = /> <UserRouter /> <ErrorHandler = /> </Express>;
Compile a Route
And you can compile a router and integrate it with your current app.
const UserRouter = <Route> <Middleware ="/user" => <Route ="/" ="GET" = /> <Route ="/settings" ="GET" = /> </Middleware> </Route>; const app = ; app;app;
Next steps
- Improve the documentation
- Implement a component to use
express.static
,express.json
andexpress.urlencoded