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