express-create-fetch-request
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Express createFetchRequest

CI Tests status

This package is just about a simple function used to create a fetch request from an Express request.

This is needed for instance for a React project with SSR (https://reactrouter.com/en/main/routers/create-static-handler#createstatichandler).

Install

npm install express-create-fetch-request

Usage

import { createFetchRequest } from 'express-create-fetch-request';

export async function myExpressMiddleware(req, res) {
  const fetchRequest = createFetchRequest(req, res);
  ...
}

React SSR Example

import React from 'react';
import { StaticRouterProvider } from 'react-router-dom/server';
import { createStaticHandler, createStaticRouter } from 'react-router-dom/server';
import { createFetchRequest } from 'express-create-fetch-request';
import Component, { loader, ErrorBoundary } from './my-app';

const routes = [{ path: '/', loader, Component, ErrorBoundary }];

export async function renderHtml(req, res) {
  const { query, dataRoutes } = createStaticHandler(routes);
  const fetchRequest = createFetchRequest(req, res);
  const context = await query(fetchRequest);

  // If we got a redirect response, short circuit and let our Express server
  // handle that directly
  if (context instanceof Response) {
    throw context;
  }

  const router = createStaticRouter(dataRoutes, context);
  return ReactDOMServer.renderToString(
    <React.StrictMode>
      <StaticRouterProvider router={router} context={context} />
    </React.StrictMode>
  );
}

Credits

Author

Adrien Febvay https://github.com/adrien-febvay

Package Sidebar

Install

npm i express-create-fetch-request

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

9.94 kB

Total Files

5

Last publish

Collaborators

  • afebvay