Giter Site home page Giter Site logo

reactend-express's Introduction

Reactend / Express

React-like http-server on Nodejs


Planet Express

What's that?

  • Node.js http-server based on React-Components
  • Express.js inside
  • Get, Post, Delete and etc. components to use router method
  • Get(render) and Res.Render to render your regular React DOM Components
  • useContext(ReqResContext) hook to access req, res
  • Support styled-components
  • Built-in logger (morgan)
  • Middleware component in Router and its Routes
  • handler prop in Route components to use as regular controller

and many many features that should be documented...

Get started

Run this to create reactend project on your local machine

npx create-reactend my-app

You choose template (default: basic)

npx create-reactend my-app --template faker

Code Example

import React from 'react';
import { resolve } from 'path';

import { registerApp, App, Static, Router, Get, Post, Res, Logger } from '@reactend/express';

const ExpressApp = () => (
  <App port={process.env.PORT || 8080}>
    <Static publicPath={resolve(__dirname, '/public')} />
    <Logger mode="dev" />
    <Router path="/">
      <Get>
        <Res.Header name="Cache-Control" value="public, max-age=31557600" />
        <Res.Render component={HomePage} />
      </Get>
      <Get path="/components" render={ComponentsPage} />
      <Router path="/api">
        <Post
          path="/status"
          json={{ msg: 'It is okay, bro' }}
          handler={(req) => console.log(req.originalUrl)}
        />
      </Router>
      <Get path="*" text="Not Found" status={404} />
    </Router>
  </App>
);

registerApp(ExpressApp);

You can use this way too

import cors from 'cors';
<Middleware handler={cors()} />;
<Get path="/redirect">
  <Res.Redirect statusCode={301} path="https://ru.reactjs.org" />
</Get>

<Post path="/json">
  <Res.Status statusCode={401} />
  <Res.Content json={{ msg: 'No Access' }} contentType="application/json" />
</Post>

<Get path="/send-file">
  <Res.SendFile path={resolve('public/code-example.png')} onError={console.log} />
</Get>

<Get path="/render">
  <Res.Render component={() => <h1>Shut Up And Take My Money!</h1>} />
</Get>

<Get path="/faker">
  <Res.Faker
    length={10}
    locale="az"
    map={{
      fullName: "name.findName",
      avatar: "image.people",
      registeredAt: "date.past",
    }}
  />
</Get>

Components

This minor description for now (Docs is on the way)

<App /> - App Instance (props: port)
<Static /> - Static route (props: publicPath, path, options)
<Router /> - Router-Provider (props: path)
<Get />, <Post /> and ... - Route component (props: path, content,
handler, status)
<Middleware /> - Middleware (props: handler)
<Logger /> - morgan logger (props: mode, disabled)
<Res /> - Response components
<Res.Render /> - Render (props: component)
<Res.Content /> - Response send (props: json, text, contentType)
<Res.Status /> - Response Status (props: statusCode)
<Res.SendFile /> - Response Send File (props: path, options,
onError)
<Res.Redirect /> - Redirect (props: path, statusCode)
<Res.Faker /> - Redirect (props: length, locale, map)



Contact me

Email me if you have any idea and you would like to be contributor [email protected]

Cheers โœจ

reactend-express's People

Contributors

gigantz avatar orkhan-huseyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reactend-express's Issues

Fetch data before returning components

Can we fetch data before returning the Component? Can I do something like this?

<Get path="/status" handler={async (req, res) => {
     const data = await getData();
     return <Res.Render component={() => <div>I am from {data.status}</div>} />
}} />

Roadmap

Hey there,

  • Routes' children as a res params <Get path="/"><Res.Content json={} text="" ... /></Get>
  • Prepare environment to build package
  • Caching
  • Docs/Wiki
  • React Hooks for backend
  • Support css.modules

Suggested adjustment to the API

Interresting...

The following might be slightly cleaner:

import {App, Static, Router, Get, Post, Put, Delete, Patch} from 'react-express';

Would also suggest passing a renderer function for the content...

ex:

   <Get path="/" content={() => <HomePage />} />

So that rendering of HomePage, etc is deferred until demanded.

TypeScript support

It would be great to have TypeScript support out of the box rather than @types/reactend__express. Would be happy to work on this sometime soon, but leaving this here as an RFC kind of thing.

Can we decouple express from this repo?

I was spending the day trying to decouple the express from the app (I failed ๐Ÿ˜” ).
If we can do that, it will support quite a few cases where express is not supported.
For example, Cloudflare workers.

Polka / tinyhttp integration

Would be cool to be able to have examples of using react-xpress with other Express like frameworks (if it's possible)

also it'd be better if express would be a peer dep so it's easier to alias modules (like when you replace React with Preact in a Next.js app)

How to use without template

I'm trying to implement it alongside my existing project, I don't want to start from scratch with the template.

However I'm getting an error, could you please provide information on how to use it ?

image

How to test locally?

Hi,

so I'm trying to test this project locally but can't seem to get React working. When I use JSX, there's an SyntaxError, when I rename the file to .jsx, there's an TypeError:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".jsx" for /path/to/the/file.jsx

maybe you could add a little tutorial on how to setup everything locally? This way, everyone could contribute to this project :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.