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

tilt-react-views

v0.1.0

Published

An Express like API to render React JSX Views with bare http.Response

Downloads

82

Readme

tilt-react-views Build Status

This module provides a ReactViews class to extend or mixin, that exposes the necessary methods to compile and render React components from an HTTP response.

Inspiration from express-react-views. This package aims to offer a similar experience, but without Express.

Install

npm i tilt-react-views -S

Usage

Views are JSX files automatically transpiled by babel-register, stored in the views directory (as configured view .views() or options.views).

The default value is ./views in the current working directory.

views/index.jsx

var React = require('react');

var HelloMessage = React.createClass({
  render: function() {
    return (<div>Hello {this.props.name}</div>);
  }
});

module.exports = HelloMessage;

server.js

const path       = require('path');
const ReactViews = require('tilt-react-views');

var views = new ReactViews({
  views: path.join(__dirname, 'views')
});

require('http').createServer((req, res) => {
  res.render = views.render.bind(views, req, res, next);
  res.render('index');
}).listen(3000);

Or within the stack of middleware, prior to the router handlers. This adds a .render() method to the response object.

const views = new ReactViews();
app.use((res, res, next) => {
  res.render = views.render.bind(views, req, res, next);
  next();
});

app.get('/', (req, res, next) => {
  // will render views/index.jsx
  res.render('index');
});

Layouts

You can compose your views to decorate your component with a layout.

views/index.jsx

var React  = require('react');
var Layout = require('./layout');

var HelloMessage = React.createClass({
  render: function() {
    return (
      <Layout title={this.props.title}>
        <div>Hello {this.props.name}</div>
      </Layout>
    );
  }
});

module.exports = HelloMessage;

views/layout.jsx

var React = require('react');

module.exports = React.createClass({
  render: function() {
    return (
      <html>
        <head><title>{this.props.title}</title></head>
        <body>{this.props.children}</body>
      </html>
    );
  }
});

Documentation

new ViewsReact(options)

  • options
    • options.views: Views directory to lookup from when rendering (default: 'view/')
    • options.engine: View extension to use (default: jsx)
    • options.doctype: Doctype to use with HTML markup (default: <!doctype html>)
    • options.renderToString: When truthy, force the output to be done with renderToString (default: renderToStaticMarkup)

.render(req, res, next, view, props)

Ends the response with Homepage component HTML markup

res.render = views.render.bind(views, req, res, next);
res.render('homepage', {
  title: 'Homepage',
  description: 'Server side react rendering'
});

.views(path)

Set the views directory to seach template from.

.engine(ext)

Get or set the file extension to use when loading views.

.react(filename, options)

Require the transpiled component by Babel and creates a new React element. The resulting HTML is sent back to the client.

Tests

Run with npm test.

var views = new ViewsReact();
views.views(path.join(__dirname, 'views'));

var app = http.createServer((req, res) => {
  res.render = views.render.bind(views, req, res, done);
  res.render('index', { title: 'Foo' });
});

request(app)
  .get('/')
  .expect(/<!doctype html>/)
  .expect(/<html/)
  .expect(/<head>/)
  .expect(/Foo<\/title>/)
  .expect(/Hello/)
  .expect(200, done);

MIT  ·  mkla.bz  ·  @mklabs