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.
- โ๏ธ Lightweight: only 8 KiB minified and gzipped.
- โจ Serverless ready: works even in browser.
- ๐ก Security oriented: uses the most strict Content Securiy Policy (CSP) by default.
- ๐ Standards based: uses WebAPI interfaces.
- ๐ณ Transport agnostic: no HTTP or platform coupling, ship requests via everything.
# Install plant web server
npm i @plant/plant
# Install node HTTP2 transport
npm i @plant/http2
Hello world with HTTP2 as transport.
โ ๏ธ Note that default CSP header value isdefault-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 on Mozilla Developer Network
// 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)
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.
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)
Hello world with HTTP2 as transport.
// 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 = '<html><script src="/script.js"></script></html>'
})
createServer(plant)
.listen(8080)
Router @plant/router
Plant standalone router.
HTTP2 @plant/http2
Plant adapter for native node.js http2 module server. It creates server
listener from Plant instance and http2.createServer()
options. It's
usage is the same as https module.
HTTPS2 @plant/https2
Plant adapter for native node.js http2 module SSL server. It creates server
listener from Plant instance and http2.createSecureServer()
options. It's
usage is the same as https module.
HTTP @plant/http
Plant adapter for native node.js http module. It creates server listener from plant instance.
HTTPS @plant/https
Plant adapter for native node.js https module. It creates server listener from plant instance and https options.
HTTP Adapter @plant/http-adapter
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 @plant/electron
This package is using to connect Plant and with current Electron instance protocols API.
It's using electron-adapter
under the hood.
Electron Adapter @plant/electron-adapter
This package is using to connect Plant and with Electron protocols API.
Flow @plant/flow
This is library for cascades. This is where contexts manage take place and requests pass from one handler to another.
Node Stream Utils @plant/node-stream-utils
Node <-> WebAPI streams adapters. Useful for wrapping Node.js streams to work with Plant.
Test HTTP Suite @plant/test-http
Tiny package with tools for HTTP testing. It simplify server creation and request sending and receiving.
MIT ยฉ Rumkin