micro-react
Create microservice apps with React components
npm i micro-react
Usage
Create a server-side compatible React component
// App.jsconst React = const App = <h1>Hello</h1> moduleexports = App
Add a start script to package.json
"scripts":
Start the server with npm start
Request Object
The Node.js http request object is passed as props.req
const React = const App = <h1>Hello propsrequrl</h1> moduleexports = App
Async Components
Use async functions to fetch data and handle other asynchronous tasks before rendering.
const React = const fetch = const App = async { const res = await const data = await res return <h1>Hello data</h1> } moduleexports = App
Client-side JS
By default micro-react only serves static HTML.
Pass the --bundle
flag to create a browser-compatible bundle on start,
that will be sent in the request after the React Node stream has finished.
micro-react App.js --bundle
See the examples for more.