https://github.com/rumkin/plant
๐ณ JS web server charged with WebAPI and neat HTTP2 support
https://github.com/rumkin/plant
http javascript js server
Last synced: 8 months ago
JSON representation
๐ณ JS web server charged with WebAPI and neat HTTP2 support
- Host: GitHub
- URL: https://github.com/rumkin/plant
- Owner: rumkin
- Created: 2017-06-26T20:14:13.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T20:16:02.000Z (over 2 years ago)
- Last Synced: 2024-10-23T11:57:10.251Z (9 months ago)
- Topics: http, javascript, js, server
- Language: JavaScript
- Homepage: https://npmjs.com/package/@plant/plant
- Size: 1.15 MB
- Stars: 80
- Watchers: 7
- Forks: 6
- Open Issues: 37
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
![]()
# Plant
[](https://npmjs.com/package/@plant/plant)
[](https://npmjs.com/package/@plant/plant)
[NPM](https://npmjs.com/package/@plant/plant) ยท
[Source](packages/plant) ยท [Readme](packages/plant/readme.md)Plant is WebAPI standards based HTTP2 web server, created with
modular architecture and functional design in mind. Also it's pure and less coupled.Plant supports HTTP 1 and HTTP 2 protocols. But it's transport agnostic and can work right
in the browser over WebSockets, WebRTC, or PostMessage.## Features
- โ๏ธ Lightweight: only **8** KiB minified and gzipped.
- โจ Serverless ready: works even in browser.
- ๐ก Security oriented: uses the most strict [Content Securiy Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) (CSP) by default.
- ๐ Standards based: uses WebAPI interfaces.
- ๐ณ Transport agnostic: no HTTP or platform coupling, ship requests via __everything__.---
## Table of Contents
* [Install](#install)
* [Examples](#exmaples)
* [Packages](#packages)
* [Internal packages](#internal-packages)## Install
```bash
# Install plant web server
npm i @plant/plant
# Install node HTTP2 transport
npm i @plant/http2
```## Examples
### Hello World
Hello world with HTTP2 as transport.
> โ ๏ธ Note that default CSP header value is `default-src localhost; form-action localhost`.
> This will prevent web page from loading any external resource at all.
> Set minimal required CSP on your own. Read about [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) on Mozilla Developer Network```javascript
// Build request handler
const createServer = require('@plant/http2');
const Plant = require('@plant/plant');const plant = new Plant();
plant.use(({res}) => {
res.body = 'Hello, World!'
})createServer(plant)
.listen(8080)
```### Router
Plant's builtin router is extremely simple and works only with
exact strings. But there is more powerful router package which brings named params and regular expressions into routing.```javascript
const Plant = require('@plant/plant');
const Router = require('@plant/router');const plant = new Plant()
const router = new Router()router.get('/user/:name', async function({res, route}) {
res.body = `Hello, ${route.params.name}!`
})plant.use('/api/v1/*', router)
```### HTTP2 pushes
Hello world with HTTP2 as transport.
```javascript
// Build request handler
const createServer = require('@plant/http2');
const Plant = require('@plant/plant');const plant = new Plant();
plant.use('/script.js', ({res}) => {
res.headers.set('content-type', 'application/javascript')
res.body = 'console.log("Hello")'
})plant.use('/index.html', ({res, fetch}) => {
// Push '/script.js' URL to pushed resources.
// It will be requested before sending main response.
res.push('/script.js')
// ... or ...
// Push complete response from subrequest
res.push(
await fetch('/script.js')
)res.body = ''
})createServer(plant)
.listen(8080)
```## Packages
### [Router](packages/router) `@plant/router`
[NPM](https://npmjs.com/package/@plant/router) ยท
[Source](packages/router) ยท [Readme](packages/router/readme.md)Plant standalone router.
## HTTP(S) Packages
### [HTTP2](packages/http2) `@plant/http2`
[NPM](https://npmjs.com/package/@plant/http2) ยท
[Source](packages/http2) ยท [Readme](packages/http2/readme.md)Plant adapter for native node.js http2 module server. It creates server
listener from Plant instance and `http2.createServer()` [options](https://nodejs.org/dist/latest-v11.x/docs/api/http2.html#http2_http2_createserver_options_onrequesthandler). It's
usage is the same as https module.### [HTTPS2](packages/https2) `@plant/https2`
[NPM](https://npmjs.com/package/@plant/https2) ยท
[Source](packages/https2) ยท [Readme](packages/https2/readme.md)Plant adapter for native node.js http2 module SSL server. It creates server
listener from Plant instance and `http2.createSecureServer()` [options](https://nodejs.org/dist/latest-v11.x/docs/api/http2.html#http2_http2_createsecureserver_options_onrequesthandler). It's
usage is the same as https module.### [HTTP](packages/http) `@plant/http`
[NPM](https://npmjs.com/package/@plant/http) ยท
[Source](packages/http) ยท [Readme](packages/http/readme.md)Plant adapter for native node.js http module. It creates server listener from plant instance.
### [HTTPS](packages/https) `@plant/https`
[NPM](https://npmjs.com/package/@plant/https) ยท
[Source](packages/https) ยท [Readme](packages/https/readme.md)Plant adapter for native node.js https module. It creates server listener from plant instance and https options.
### [HTTP Adapter](packages/http-adapter) `@plant/http-adapter`
[NPM](https://npmjs.com/package/@plant/http-adapter) ยท
[Source](packages/http-adapter) ยท [Readme](packages/http-adapter/readme.md)This package is using to connect Plant and native Node's HTTP server. Modules http, https, http2, and https2 use it under the hood.
## Electron Packages
### [Electron](packages/electron-adapter) `@plant/electron`
[NPM](https://npmjs.com/package/@plant/electron) ยท
[Source](packages/electron) ยท [Readme](packages/electron/readme.md)This package is using to connect Plant and with current Electron instance protocols API.
It's using `electron-adapter` under the hood.### [Electron Adapter](packages/electron-adapter) `@plant/electron-adapter`
[NPM](https://npmjs.com/package/@plant/electron-adapter) ยท
[Source](packages/electron-adapter) ยท [Readme](packages/electron-adapter/readme.md)This package is using to connect Plant and with Electron protocols API.
## Utility Packages
### [Flow](packages/flow) `@plant/flow`
[NPM](https://npmjs.com/package/@plant/flow) ยท
[Source](packages/flow) ยท [Readme](packages/flow/readme.md)This is library for cascades. This is where contexts manage take place and requests pass from one handler to another.
### [Node Stream Utils](packages/node-stream-utils) `@plant/node-stream-utils`
[NPM](https://npmjs.com/package/@plant/node-stream-utils) ยท
[Source](packages/node-stream-utils) ยท [Readme](packages/node-stream-utils/readme.md)Node <-> WebAPI streams adapters. Useful for wrapping Node.js streams to work
with Plant.## Tests Packages
### [Test HTTP Suite](packages/test-http) `@plant/test-http`
[NPM](https://npmjs.com/package/@plant/test-http) ยท
[Source](packages/test-http) ยท [Readme](packages/test-http/readme.md)Tiny package with tools for HTTP testing. It simplify server creation and request sending and receiving.
## License
MIT ยฉ [Rumkin](https://rumk.in)