npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-middleware

v2.2.4

Published

Connect middleware for serving React components from a standard folder structure.

Downloads

52

Readme

react-middleware

Build Status

Connect middleware for serving React components from a standard folder structure.

Getting Started

npm install --save react-middleware

Once the module is added to your project, you can initialize the convention base folder structure using the init method:

import ReactMiddleware from "react-middleware";
ReactMiddleware.init("./site");

The init method need only be called once, and it lays down the following folder structure within the base folder:

/site
  |-- routes.js         # Page routes.
  |-- css               # Global stylesheets.
  |-- scripts           # Global javascript (eg. analytics).
  |-- public            # Static assets.
  |-- views
      |-- components    # Reusable UI components.
      |-- layouts       # Root level page layouts.
      |-- pages         # Specific pages.

From here you can start the server in the following ways:

Instance Helper on Middleware
const middleware = ReactMiddleware({ base:"./site" });
middleware.start(3030)
.then(() => {
  // Startup complete (callback).
});
Static Helper on Middleware

This option is useful if you want to incorporate your react-middleware site within a wider Express application.

const app = express();
const middleware = ReactMiddleware({ base:"./example/site" });
ReactMiddleware.start(app, middleware, { port: 3030 })
.then(() => {
  // Startup complete (callback).
});

Options for starting:

  • port: The port to run the app on.
  • name: The display name of the application (emitted to console).
  • version: The version of the application (emitted to console).
Using Express

Alternatively you can start the server using Express without the convenience methods shown above:

const app = express();
const site = ReactMiddleware({ base:"./example/site" });
site.build()
.then(() => {
    app
      .use(site)
      .listen(3030, () => {
          console.log("Listening on port", 3030);
      });
});

Notice the build() step that ensures all the static assets (js,css) have been compiled.

Passing Custom Loaders

Sometimes you need to pass in custom webpack loaders:

const webpackLoaders = [
  {
    test: /\.js$/,
    exclude: /(node_modules)/,
    loader: "babel",
    query: {
      plugins: [ fsPath.join(__dirname, "relay-plugin") ]
    }
  },
  { test: /\.json$/, loader: "json" }
];

const site = ReactMiddleware({ base:"./example/site", webpackLoaders });

This will replace the default set of loaders with the given array.

Passing Custom Logger

The ReactMiddleware logs startup information. To have this write to a customer logger (eg Winston), you can pass a logger to the middleware at initialization:

import winston from "winston";
const logger = new (winston.Logger)({ ... });
const site = ReactMiddleware({ base:"./example/site", logger });

The logger object should expose .info, .warn and .error methods.

CSS

The module works on the philosophy that styles, if not within the HTML component itself, should be as damn close to the corresponding component as possible. CSS and layup are two sides of the same coin - they are not seperate concerns.

Situate .styl or .css files next to your Page.jsx or Component.jsx and the server will automatically find and compile it into production-ready CSS.

Global CSS

In the cases where you need global CSS, such as resets and common page/class styles, place these within the folder:

/base
  |-- /css # Global CSS.

This folder is automatically populated with the normalize.css reset file.

Referencing CSS

To bring CSS into the served page use the /css route, for example:

<head>
  <!--
    The "/css" path combines all CSS across
        - global
        - layouts
        - pages
        - components
  -->
  <link href="/css" rel="stylesheet"/>
</head>

To selectively bring in a subset of site's CSS pass a query-string:

  • ?global - includes the CSS within the <base>/css folder.
  • Layouts
    • ?layouts - includes all CSS within the /views/layouts folder.
    • ?layout=<Name> - includes only the named layout or comma-seperated list of layout names.
  • Pages
    • ?pages - includes all CSS within the /views/pages folder.
    • ?page=<Name> - includes only the named page or comma-seperated list of page names.
  • Components
    • ?components - includes all CSS within the /views/components folder.
    • ?component=<Name> - includes only the named component or comma-seperated list of component names.

For example:

<head>
  <link href="/css?global&components&page=Features" rel="stylesheet"/>
</head>

Some common CSS paths are provided:

<link href="/css/common" /><!-- Global, layouts, components -->
<link href="/css/global" /><!-- Global css. -->

<link href="/css/layouts" /><!-- All layouts -->
<link href="/css/layout/:Name1,:Name2" /><!-- The specified layout or comma-seperated list of layouts -->

<link href="/css/pages" /><!-- All pages -->
<link href="/css/page/:Name1,:Name2" /><!-- The specified page or comma-seperated list of pages -->

<link href="/css/components" /><!-- All components -->
<link href="/css/component/:Name1,:Name2" /><!-- The specified page or comma-seperated list of pages -->