UNPKG

6 kBMarkdownView Raw
1# **RE**serve
2
3<table border="0" cellpadding="2" cellspacing="0">
4 <tr>
5 <td valign="top">
6 <strong>RE</strong>
7 </td>
8 <td>
9 <i>duced</i></br />
10 <i>levant</i></br />
11 <i>verse proxy</i><br />
12 <i>gexp-based</i><br />
13 <i>useable</i><br />
14 <strong>serve</strong>
15 </td>
16 </tr>
17</table>
18
19[![Node.js CI](https://github.com/ArnaudBuchholz/reserve/actions/workflows/node.js.yml/badge.svg)](https://github.com/ArnaudBuchholz/reserve/actions/workflows/node.js.yml)
20[![Travis-CI](https://travis-ci.org/ArnaudBuchholz/reserve.svg?branch=master)](https://travis-ci.org/ArnaudBuchholz/reserve#)
21[![Coverage Status](https://coveralls.io/repos/github/ArnaudBuchholz/reserve/badge.svg?branch=master)](https://coveralls.io/github/ArnaudBuchholz/reserve?branch=master)
22[![Maintainability](https://api.codeclimate.com/v1/badges/49e3adbc8f31ae2febf3/maintainability)](https://codeclimate.com/github/ArnaudBuchholz/reserve/maintainability)
23[![Package Quality](https://npm.packagequality.com/shield/reserve.svg)](https://packagequality.com/#?package=reserve)
24[![Known Vulnerabilities](https://snyk.io/test/github/ArnaudBuchholz/reserve/badge.svg?targetFile=package.json)](https://snyk.io/test/github/ArnaudBuchholz/reserve?targetFile=package.json)
25[![reserve](https://badge.fury.io/js/reserve.svg)](https://www.npmjs.org/package/reserve)
26[![reserve](http://img.shields.io/npm/dm/reserve.svg)](https://www.npmjs.org/package/reserve)
27[![install size](https://packagephobia.now.sh/badge?p=reserve)](https://packagephobia.now.sh/result?p=reserve)
28[![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
29[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FArnaudBuchholz%2Freserve.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FArnaudBuchholz%2Freserve?ref=badge_shield)
30[![Documentation](https://img.shields.io/badge/-documentation-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/README.md)
31[![History](https://img.shields.io/badge/-history-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/history.md)
32
33
34A **lightweight** web server statically **configurable** with regular expressions.
35It can also be **embedded** and **extended**.
36
37# Rational
38
39Initially started to build a local **development environment** where static files are served and resources can be fetched from remote repositories, this **tool** is **versatile** and can support different scenarios :
40- A simple web server
41- A reverse proxy to an existing server
42- A server that aggregates several sources
43- ...
44
45By defining **an array of mappings**, one can decide how the server will process the requests. Each mapping associates **matching** criteria *(method selection, url matching using
46[regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))* to a **handler** that will answer the request.
47
48The configuration syntax favors **simplicity** without dropping flexibility.
49
50For instance, the definition of a server that **exposes files** of the current directory but **forbids access** to the directory `private` consists in :
51
52```json
53{
54 "port": 8080,
55 "mappings": [{
56 "match": "^/private/.*",
57 "status": 403
58 }, {
59 "match": "^/(.*)",
60 "file": "./$1"
61 }]
62}
63```
64
65# Usage
66
67## npm start
68
69* Install the package with `npm install reserve` *(you decide if you want to save it as development dependency or not)*
70* You may create a start script in `package.json` :
71
72```json
73{
74 "scripts": {
75 "start": "reserve"
76 }
77}
78```
79
80* By default, it will look for a file named `reserve.json` in the current working directory
81* A configuration file name can be specified using `--config <file name>`, for instance :
82
83```json
84{
85 "scripts": {
86 "start": "reserve",
87 "start-dev": "reserve --config reserve-dev.json"
88 }
89}
90```
91
92## Global
93
94* Install the package with `npm install reserve --global`
95* Run `reserve`
96 * By default, it will look for a file named `reserve.json` in the current working directory
97 * A configuration file name can be specified using `--config <file name>`
98
99**NOTE** : if [`process.send`](https://nodejs.org/api/process.html#process_process_send_message_sendhandle_options_callback) is defined, REserve will notify the parent process when the server is ready by sending the message `'ready'`.
100
101## Embedded
102
103It is possible to implement the server in an application using the `serve` export :
104
105```javascript
106const path = require('path')
107const { check, serve } = require('reserve')
108check({
109 port: 8080,
110 mappings: [{
111 match: /^\/(.*)/,
112 file: path.join(__dirname, '$1')
113 }]
114})
115 .then(configuration => {
116 serve(configuration)
117 .on('ready', ({ url }) => {
118 console.log(`Server running at ${url}`)
119 })
120 })
121```
122
123The resulting object implements the [EventEmitter](https://nodejs.org/api/events.html) class and throws events with parameters, see [Server events](doc/events.md).
124It also exposes a `close` method (returning a `Promise`) to shutdown the server.
125
126The package also gives access to the configuration reader :
127
128```javascript
129const path = require('path')
130const { read, serve } = require('reserve')
131read('reserve.json')
132 .then(configuration => {
133 serve(configuration)
134 .on('ready', ({ url }) => {
135 console.log(`Server running at ${url}`)
136 })
137 })
138```
139
140And a default log output *(verbose mode will dump all redirections)* :
141
142```javascript
143const path = require('path')
144const { log, read, serve } = require('reserve')
145read('reserve.json')
146 .then(configuration => {
147 log(serve(configuration), /*verbose: */ true)
148 })
149```
150
151**NOTE** : `log` is using [`colors`](https://www.npmjs.com/package/colors) **if installed**.
152
153# Complete documentation
154
155Go to this [page](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/README.md) to access documentation and articles about REserve.
156