GithubHelp home page GithubHelp logo

egx's Introduction

Egx

An ExpressJS Got Htmx and JSX.

ExpressJS is a Fast, unopinionated, minimalist web framework.

Htmx gives you access to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypertext.

JSX it's just JavaScript XML. JSX makes it easier to write markup HTML.

Features

  • Hooks support (useRequest, useResponse, useBody, useParams, useQuery, etc.).
  • Helmet support.
  • AsyncComponent support.
  • CleanupComponent support.

Install

Starter Template

npx degit herudi/egx/starter my-app
cd my-app
npm install

// run dev
npm run dev

// build
npm run build

// run prod
npm run start

Install Manually

npm i egx

Basic Usage

/** @jsx h */
/** @jsxFrag h.Fragment */

import express from "express";
import { egx, h, Helmet } from "egx";

const app = express();

app.use(egx());

app.get("/", (req, res) => {
  res.egx(
    <div>
      <Helmet>
        <title>Welcome Home</title>
      </Helmet>
      <button hx-post="/clicked" hx-swap="outerHTML">Click Me</button>
    </div>,
  );
});

app.post("/clicked", (req, res) => {
  res.egx(<h1>It's Me</h1>);
});

// handling promise
app.get("/pet", async (req, res, next) => {
  try {
    await res.egx(<h1>Cat</h1>);
  } catch (err) {
    next(err);
  }
});

app.listen(3000, () => {
  console.log("> Running on port 3000");
});

Using Hooks

useRequest

const Foo = () => {
  const { url } = useRequest();
  return <h1>{url}</h1>;
};

useResponse

const Foo = () => {
  const res = useResponse();
  res.set("my-header", "value");
  return <h1>hello</h1>;
};

useParams

type User = { name: string };

const Foo = () => {
  const { name } = useParams<User>();
  return <h1>{name}</h1>;
};

app.get("/user/:name", (req, res) => {
  res.egx(<Foo />);
});

useQuery

type User = { name: string };

const Foo = () => {
  const { name } = useQuery<User>();
  return <h1>{name}</h1>;
};

app.get("/user", (req, res) => {
  res.egx(<Foo />);
});

// GET /user?name=john

useBody

type User = { name: string };

const UserPost = async () => {
  const user = useBody<User>();
  // example save to db
  await db.user.save(user);

  return <h1>{user.name}</h1>;
};

app.post("/user", (req, res) => {
  res.egx(<UserPost />);
});

Using Cleanup Component

A cleanup component for any custom send. for example: json/redirect.

Example Sign and redirect

const Sign = async () => {
  const req = useRequest();
  const res = useResponse();

  const token = await getToken(req.body);

  if (token) {
    // use cleanup to redirect
    return () => {
      res.cookie("user", token).redirect("/admin");
    };
  }
  return <SignPost message="login failure" />;
};

Example Auth and NextFunction

const Auth: FC = (props) => {
  const req = useRequest();
  const next = useNext();

  if (req.isLogin === false) {
    // use cleanup to next function
    return () => {
      next(new Error("user not found"));
    };
  }

  return props.children;
};

app.get("/", (req, res) => {
  res.egx(
    <Auth>
      <Home />
    </Auth>,
  );
});

License

MIT

egx's People

Contributors

herudi avatar

Stargazers

 avatar

Watchers

 avatar

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.