UNPKG

6.01 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[![Travis-CI](https://travis-ci.org/ArnaudBuchholz/reserve.svg?branch=master)](https://travis-ci.org/ArnaudBuchholz/reserve#)
20[![Coverage Status](https://coveralls.io/repos/github/ArnaudBuchholz/reserve/badge.svg?branch=master)](https://coveralls.io/github/ArnaudBuchholz/reserve?branch=master)
21[![Maintainability](https://api.codeclimate.com/v1/badges/49e3adbc8f31ae2febf3/maintainability)](https://codeclimate.com/github/ArnaudBuchholz/reserve/maintainability)
22[![Package Quality](https://npm.packagequality.com/shield/reserve.svg)](https://packagequality.com/#?package=reserve)
23[![Known Vulnerabilities](https://snyk.io/test/github/ArnaudBuchholz/reserve/badge.svg?targetFile=package.json)](https://snyk.io/test/github/ArnaudBuchholz/reserve?targetFile=package.json)
24[![dependencies Status](https://david-dm.org/ArnaudBuchholz/reserve/status.svg)](https://david-dm.org/ArnaudBuchholz/reserve)
25[![devDependencies Status](https://david-dm.org/ArnaudBuchholz/reserve/dev-status.svg)](https://david-dm.org/ArnaudBuchholz/reserve?type=dev)
26[![reserve](https://badge.fury.io/js/reserve.svg)](https://www.npmjs.org/package/reserve)
27[![reserve](http://img.shields.io/npm/dm/reserve.svg)](https://www.npmjs.org/package/reserve)
28[![install size](https://packagephobia.now.sh/badge?p=reserve)](https://packagephobia.now.sh/result?p=reserve)
29[![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
30[![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)
31[![Documentation](https://img.shields.io/badge/-documentation-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/README.md)
32[![History](https://img.shields.io/badge/-history-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/history.md)
33
34
35A **lightweight** web server statically **configurable** with regular expressions.
36It can also be **embedded** and **extended**.
37
38# Rational
39
40Initially 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 :
41- A simple web server
42- A reverse proxy to an existing server
43- A server that aggregates several sources
44- ...
45
46By 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
47[regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))* to a **handler** that will answer the request.
48
49The configuration syntax favors **simplicity** without dropping flexibility.
50
51For instance, the definition of a server that **exposes files** of the current directory but **forbids access** to the directory `private` consists in :
52
53```json
54{
55 "port": 8080,
56 "mappings": [{
57 "match": "^/private/.*",
58 "status": 403
59 }, {
60 "match": "^/(.*)",
61 "file": "./$1"
62 }]
63}
64```
65
66# Usage
67
68## npm start
69
70* Install the package with `npm install reserve` *(you decide if you want to save it as development dependency or not)*
71* You may create a start script in `package.json` :
72
73```json
74{
75 "scripts": {
76 "start": "reserve"
77 }
78}
79```
80
81* By default, it will look for a file named `reserve.json` in the current working directory
82* A configuration file name can be specified using `--config <file name>`, for instance :
83
84```json
85{
86 "scripts": {
87 "start": "reserve",
88 "start-dev": "reserve --config reserve-dev.json"
89 }
90}
91```
92
93## Global
94
95* Install the package with `npm install reserve --global`
96* Run `reserve`
97 * By default, it will look for a file named `reserve.json` in the current working directory
98 * A configuration file name can be specified using `--config <file name>`
99
100**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'`.
101
102## Embedded
103
104It is possible to implement the server in an application using the `serve` export :
105
106```javascript
107const path = require('path')
108const { check, serve } = require('reserve')
109check({
110 port: 8080,
111 mappings: [{
112 match: /^\/(.*)/,
113 file: path.join(__dirname, '$1')
114 }]
115})
116 .then(configuration => {
117 serve(configuration)
118 .on('ready', ({ url }) => {
119 console.log(`Server running at ${url}`)
120 })
121 })
122```
123
124The resulting object implements the [EventEmitter](https://nodejs.org/api/events.html) class and throws events with parameters, see [Server events](doc/events.md).
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