Giter Site home page Giter Site logo

lyuji282 / react-ssr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from saltyshiomix/react-ssr

0.0 1.0 0.0 760 KB

React SSR as a view template engine

Home Page: https://yarn.pm/@react-ssr/express

License: MIT License

JavaScript 35.27% TypeScript 64.73%

react-ssr's Introduction

๐Ÿ˜Ž React SSR ๐Ÿ˜Ž

React SSR as a view template engine

How to use

Install it:

$ npm install --save @react-ssr/express express react react-dom

and add a script to your package.json like this:

{
  "scripts": {
    "start": "node server.js"
  }
}

Populate below files inside your project:

./server.js

const express = require('@react-ssr/express');
const app = express();

app.get('/', (req, res) => {
  const user = { name: 'World' };
  res.render('index', { user });
});

app.listen(3000, () => {
  console.log('> Ready on http://localhost:3000');
});

./views/index.jsx

import React from 'react';

export default function Index({ user }) {
  return `Hello ${user.name}!`;
}

and then just run npm start and go to http://localhost:3000.

You'll see Hello World!.

Features

  • Pass server data to the client props
  • Auto SSR (Server Side Rendering)
  • TypeScript support

Configuration

const express = require('@react-ssr/express');

// default configuration
const app = express({
  distDir: 'dist',
  viewsDir: 'views',
});

TypeScript Support

To enable TypeScript .tsx, just put tsconfig.json in your project root directory.

And TypeScript codes should be like these:

./package.json

{
  "scripts": {
    "start": "ts-node server.ts"
  }
}

./server.ts

import express from '@react-ssr/express';
import { Request, Response } from 'express';

const app = express();

app.get('/', (_req: Request, res: Response) => {
  const user = { name: 'World' };
  res.render('index', { user });
});

app.listen(3000, () => {
  console.log('> Ready on http://localhost:3000');
});

./views/index.tsx

import React from 'react';

interface IndexProps {
  user: any;
}

export default function Index({ user }: IndexProps) {
  return `Hello ${user.name}!`;
}

Packages

package version
@react-ssr/express @react-ssr/express downloads

Examples

See the examples folder.

Roadmaps

  • README
    • Roadmaps
    • How to use
    • Contributing guides
  • develop packages
    • lerna monorepo development
    • @react-ssr/express
      • .jsx support
      • .tsx support
    • @react-ssr/fastify
    • @react-ssr/nest

Develop examples/<example-folder-name>

$ git clone https://github.com/saltyshiomix/react-ssr.git
$ cd react-ssr
$ yarn
$ yarn dev <example-folder-name>

Articles

The React View Template Engine for Express

react-ssr's People

Contributors

saltyshiomix avatar

Watchers

James Cloos 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.