|
1 |
| -# ReactXpress |
| 1 | +# Reactend / Express |
2 | 2 |
|
3 |
| -**React renderer to build Node.js server** |
| 3 | +React-like http-server on Nodejs |
4 | 4 | <br />
|
5 | 5 |
|
6 |
| - |
| 6 | + |
| 7 | +<br/> |
7 | 8 |
|
8 |
| -### Why? |
| 9 | +## Why? |
9 | 10 |
|
10 | 11 | It's the only crazy idea to use React to structure Backend on Node.js.
|
11 |
| -<br /> |
| 12 | +<br /><br /> |
12 | 13 |
|
13 |
| -### How it works? |
| 14 | +## How it works? |
14 | 15 |
|
15 | 16 | It works with express.js framework to run Node.js server. Custom renderer we have is building express structure app from React Components.
|
16 |
| -<br /> |
| 17 | +<br /><br /> |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +### Install the package |
| 22 | + |
| 23 | +`npm i --save @reactend/express`</br> |
| 24 | +or</br> |
| 25 | +`yarn add @reactend/express`</br> |
| 26 | +<br/> |
17 | 27 |
|
18 |
| -### Code Example |
| 28 | +### Install peer dependecies |
| 29 | + |
| 30 | +`npm i --save react react-dom react-helmet styled-components`</br> |
| 31 | +or</br> |
| 32 | +`yarn add react react-dom react-helmet styled-components`</br> |
| 33 | + |
| 34 | +</br> |
| 35 | + |
| 36 | +## Code Example |
19 | 37 |
|
20 | 38 | ```js
|
21 | 39 | import React from 'react';
|
22 | 40 | import { resolve } from 'path';
|
23 |
| -import { ReactXpress, App, Static, Router, Get, Post, Res } from '../lib'; |
24 | 41 |
|
25 |
| -const HomePage = () => <h1>Home page</h1>; |
26 |
| -const AboutPage = () => <h1>About Page</h1>; |
| 42 | +import { registerApp, App, Static, Router, Get, Post, Res, Logger } from '@reactend/express'; |
27 | 43 |
|
28 | 44 | const ExpressApp = () => (
|
29 | 45 | <App port={process.env.PORT || 8080}>
|
30 | 46 | <Static publicPath="/public" />
|
| 47 | + <Logger mode="dev" /> |
31 | 48 | <Router path="/">
|
32 |
| - <Get render={HomePage} /> |
33 |
| - <Get path="/about" render={AboutPage} /> |
| 49 | + <Get> |
| 50 | + <Res.Header name="Cache-Control" value="public, max-age=31557600" /> |
| 51 | + <Res.Render component={HomePage} /> |
| 52 | + </Get> |
| 53 | + <Get path="/components" render={ComponentsPage} /> |
34 | 54 | <Router path="/api">
|
35 | 55 | <Post
|
36 | 56 | path="/status"
|
37 | 57 | json={{ msg: 'It is okay, bro' }}
|
38 | 58 | handler={(req) => console.log(req.originalUrl)}
|
39 | 59 | />
|
40 | 60 | </Router>
|
41 |
| - <Updates /> |
42 | 61 | <Get path="*" text="Not Found" status={404} />
|
43 | 62 | </Router>
|
44 | 63 | </App>
|
45 | 64 | );
|
| 65 | +``` |
46 | 66 |
|
47 |
| -// Updates! 🤩 |
48 |
| -const Updates = () => ( |
49 |
| - <> |
50 |
| - <Get path="/redirect"> |
51 |
| - <Res.Redirect statusCode={301} path="https://ru.reactjs.org" /> |
52 |
| - </Get> |
53 |
| - <Post path="/json"> |
54 |
| - <Res.Status statusCode={401} /> |
55 |
| - <Res.Content json={{ msg: 'No Access' }} contentType="application/json" /> |
56 |
| - </Post> |
57 |
| - <Get path="/send-file"> |
58 |
| - <Res.SendFile path={resolve('public/code-example.png')} onError={console.log} /> |
59 |
| - </Get> |
60 |
| - <Get path="/render"> |
61 |
| - <Res.Render component={() => <h1>Shut Up And Take My Money!</h1>} /> |
62 |
| - </Get> |
63 |
| - </> |
64 |
| -); |
| 67 | +<br /> |
| 68 | + |
| 69 | +## You can use this way too |
65 | 70 |
|
66 |
| -ReactXpress.render(<ExpressApp />); |
| 71 | +```js |
| 72 | +import cors from 'cors'; |
| 73 | +<Middleware handler={cors()} />; |
67 | 74 | ```
|
68 | 75 |
|
69 |
| -### How to use |
70 |
| - |
71 |
| -1. Clone the repo |
72 |
| -2. `npm install` |
73 |
| -3. Run dev mode - `npm run dev` |
74 |
| -4. Do all changes in `./src` folder as it's not library yet. |
75 |
| - |
76 |
| -### Components |
77 |
| - |
78 |
| -`<App />` - App Instance (props: port) |
79 |
| -`<Static />` - Static route (props: publicPath, path, options) |
80 |
| -`<Router />` - Router-Provider (props: path) |
81 |
| -`<Get />, <Post /> and ...` - Route component (props: path, content, handler, status) |
82 |
| -`<Res />` - Response components |
83 |
| -`<Res.Render />` - Render (props: component) |
84 |
| -`<Res.Content />` - Response send (props: json, text, contentType) |
85 |
| -`<Res.Status />` - Response Status (props: statusCode) |
86 |
| -`<Res.SendFile />` - Response Send File (props: path, options, onError) |
87 |
| -`<Res.Redirect />` - Redirect (props: path, statusCode) |
| 76 | +```js |
| 77 | +<Get path="/redirect"> |
| 78 | + <Res.Redirect statusCode={301} path="https://ru.reactjs.org" /> |
| 79 | +</Get> |
| 80 | + |
| 81 | +<Post path="/json"> |
| 82 | + <Res.Status statusCode={401} /> |
| 83 | + <Res.Content json={{ msg: 'No Access' }} contentType="application/json" /> |
| 84 | +</Post> |
| 85 | + |
| 86 | +<Get path="/send-file"> |
| 87 | + <Res.SendFile path={resolve('public/code-example.png')} onError={console.log} /> |
| 88 | +</Get> |
| 89 | + |
| 90 | +<Get path="/render"> |
| 91 | + <Res.Render component={() => <h1>Shut Up And Take My Money!</h1>} /> |
| 92 | +</Get> |
| 93 | +``` |
| 94 | + |
| 95 | +<br/> |
| 96 | + |
| 97 | +## Components |
| 98 | + |
| 99 | +_This minor description for now (Docs is on the way)_<br/><br/> |
| 100 | +`<App />` - App Instance (props: port) <br /> |
| 101 | +`<Static />` - Static route (props: publicPath, path, options) <br /> |
| 102 | +`<Router />` - Router-Provider (props: path) <br /> |
| 103 | +`<Get />, <Post /> and ...` - Route component (props: path, content, <br />handler, status) <br /> |
| 104 | +`<Middleware />` - Middleware (props: handler) <br /> |
| 105 | +`<Logger />` - morgan logger (props: mode, disabled) <br /> |
| 106 | +`<Res />` - Response components <br /> |
| 107 | +`<Res.Render />` - Render (props: component) <br /> |
| 108 | +`<Res.Content />` - Response send (props: json, text, contentType) <br /> |
| 109 | +`<Res.Status />` - Response Status (props: statusCode) <br /> |
| 110 | +`<Res.SendFile />` - Response Send File (props: path, options, <br />onError) <br /> |
| 111 | +`<Res.Redirect />` - Redirect (props: path, statusCode) <br /> |
88 | 112 | <br />
|
89 | 113 | <br />
|
90 | 114 |
|
91 |
| -### What is planning? |
92 |
| - |
93 |
| -I work on it and I'm trying to improve it, even it's not a good idea to use this kinda renderer for real-world app. But It would be awesome to have contributors to make its DX much better. |
94 |
| - |
95 |
| -### Contact me |
| 115 | +## Contact me |
96 | 116 |
|
97 | 117 | Email me if you have any idea and you would like to be contributor [orkhanjafarovr@gmail.com](mailto:orkhanjafarovr@gmail.com)
|
98 |
| - |
99 |
| -Resources: <br/> |
100 |
| -https://dev.to/orkhanjafarovr/express-in-react-react-backend-whut-4lkg |
|
0 commit comments