This documentation covers the react-serve-js package and the create-react-serve CLI tool. These tools provide a React-inspired, component-based API for building backend HTTP servers using Express, with support for declarative routing, middleware, and hot reloading. The CLI helps scaffold new projects using this system.
- react-serve-js: Core runtime for building servers with React-like components.
- create-react-serve: CLI for scaffolding new projects using react-serve-js.
npm install react-serve-js
- Component-based Routing: Define routes and middleware using JSX/React components.
- Middleware: Use custom middleware components/functions.
- Context Hooks: Access request/response and share data between middleware and routes.
- Hot Reload: Automatic server restart on file changes in development.
Bootstraps the server using a React element tree.
- element: The root JSX element (usually
<App />). - Returns: The Express server instance.
Defines the application and sets the port (default: 9000). The cors prop enables CORS middleware - set to true for default configuration or provide custom CORS options.
Groups routes under a common path prefix and/or shared middleware.
Defines a route. Children is the handler function/component.
Declares a middleware function for a group or route.
Sends a response with the given status and JSON body.
Returns the current route context:
req: Express Requestres: Express Responseparams,query,body: Request datamiddlewareContext: Map for sharing data
Sets a value in the middleware context map.
Retrieves a value from the middleware context map.
type Middleware = (req: Request, next: () => any) => any;- Call
next()to continue to the next middleware/handler. - Return a value to short-circuit and send a response.
import { serve, useRoute, Response } from "react-serve-js";
function HelloRoute() {
const { query } = useRoute();
return <Response status={200} json={{ hello: query.name || "world" }} />;
}
serve(
<App
port={6969}
cors={true} // Enable CORS for all routes
>
<RouteGroup prefix="/api">
<Route method="GET" path="/hello">
{HelloRoute}
</Route>
</RouteGroup>
</App>
);- In development (
NODE_ENV !== 'production'), the server watches for.ts/.tsxfile changes and restarts automatically.
A CLI tool to scaffold new projects using react-serve-js.
npx create-react-serve <project-name>
- Prompts for project details and sets up a new directory with a basic template.
- Template includes a sample
index.tsxusing the API described above.
src/index.tsx: Entry point with example routes.package.json,tsconfig.json,README.md: Standard project files.
src/runtime.ts: Main runtime implementation (see API above).src/index.ts: Entry point for exports.
src/cli.ts: CLI entry point.src/create-app.ts: Project scaffolding logic.templates/basic/: Basic project template.
- Custom Middleware: Use
<Middleware use={fn}>inside<RouteGroup>or<Route>. - Nested RouteGroups: Compose groups for modular route organization.
- Context Sharing: Use
useSetContext/useContextto share data between middleware and handlers.
See LICENSE file for details.
Contributions are welcome! Please open issues or pull requests on GitHub.