Rax Documentation

repository·master·Indexed 27 days ago

https://github.com/alibaba/rax

A progressive framework for building universal applications with a single codebase that runs across Web, Weex, Node.js, Alibaba MiniApp, and WeChat MiniProgram. It features a driver-based rendering system with support for DOM, Server, WebGL, and Weex, as well as specialized packages for server-side rendering (rax-server, rax-server-renderer), document structure (rax-document), and state management (rax-redux).

Tokens
17.2K
Snippets
31
Records
149
Agent score
93%

What's inside Rax

  1. Create a new Rax project via CLI

    master

    You can initialize a new Rax project using the create-rax initializer via npm init. This works with npm 6 or higher.

    After initialization, navigate to the project directory, install dependencies, and start the local development server.

    $ npm init rax <YourProjectName>
    $ cd <YourProjectName>
    $ npm install
    $ npm run start
  2. Integrate rax-server with Express

    master

    To use rax-server with the Express framework, initialize the RaxServer instance and call server.render within your Express route handlers.

    const express = require('express');
    const RaxServer = require('rax-server');
    
    const PORT = 8080;
    const app = express();
    
    const server = new RaxServer({
      // ... configuration options
    });
    
    app.get('/index', (req, res) => {
      server.render(req, res, {
        page: 'index'
      });
    });
    
    app.listen(PORT, () => {
      console.log(`app listening on port ${PORT}`);
    });
  3. Use rax-document for building HTML documents

    master

    Use rax-document to provide essential components for constructing the HTML structure of your application, including head elements and body containers. This is particularly useful for SSR (Server-Side Rendering) and MPA (Multi-Page Application) setups.

    import { createElement } from 'rax';
    import { Root, Data, Style, Script } from 'rax-document';
    
    export default () => {
      return (
        <html>
          <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width,initial-scale=1"/>
            <title>ssr-document-demo</title>
            <Style />
          </head>
          <body>
            {/* root container */}
            <Root />
            {/* initial data from server side */}
            <Data />
            <Script />
          </body>
        </html>
      );
    }