Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/phly/react2psr7

Serve PSR-7 middleware applications from a React HTTP server
https://github.com/phly/react2psr7

Last synced: 3 months ago
JSON representation

Serve PSR-7 middleware applications from a React HTTP server

Awesome Lists containing this project

README

        

# react2psr7

[![Build Status](https://secure.travis-ci.org/phly/react2psr7.svg?branch=master)](https://secure.travis-ci.org/phly/react2psr7)
[![Coverage Status](https://coveralls.io/repos/github/phly/react2psr7/badge.svg?branch=master)](https://coveralls.io/github/phly/react2psr7?branch=master)

Serve PSR-7 middleware applications from [React](http://reactphp.org).

## Installation

```console
$ composer require "react/http:^0.5@dev" phly/react2psr7
```

> ### react/http
>
> react2psr7 currently requires features from the upcoming 0.5 release of
> react/http. Since that version is not yet released, you need to specify
> it manually when installing to force Composer to allow a development
> release.

## Usage

The following demonstrates creating an HTTP server using React, and using an
[Expressive](https://zendframework.github.io/zend-expressive/) application
to handle incoming requests.

```php
on('request', new ReactRequestHandler($container->get(Application::class)));

// Listen on all ports; omit second argument to restrict to localhost.
$socket->listen(1337, '0.0.0.0');
$loop->run();
```

## Serving static files

This package also provides middleware for serving static files; this can be
useful when running React as a web server, to allow serving CSS, JavaScript, and
images.

The following demonstrates using [Stratigility](https://github.com/zendframework/zend-stratigility)
to build a middleware pipeline that consumes both the static files middleware
as well as an Expressive application in order to provide a full-fledged web
server.

```php
pipe(new StaticFiles());
$pipeline->pipe($container->get(Application::class));

$http->on('request', new ReactRequestHandler($pipeline));

// Listen on all ports; omit second argument to restrict to localhost.
$socket->listen(1337, '0.0.0.0');
$loop->run();
```

(Note: you could also pipe the static files middleware into your Expressive
application.)