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 | [![dependencies Status](https://david-dm.org/ArnaudBuchholz/reserve/status.svg)](https://david-dm.org/ArnaudBuchholz/reserve)
|
26 | [![devDependencies Status](https://david-dm.org/ArnaudBuchholz/reserve/dev-status.svg)](https://david-dm.org/ArnaudBuchholz/reserve?type=dev)
|
27 | [![reserve](https://badge.fury.io/js/reserve.svg)](https://www.npmjs.org/package/reserve)
|
28 | [![reserve](http://img.shields.io/npm/dm/reserve.svg)](https://www.npmjs.org/package/reserve)
|
29 | [![install size](https://packagephobia.now.sh/badge?p=reserve)](https://packagephobia.now.sh/result?p=reserve)
|
30 | [![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
31 | [![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)
|
32 | [![Documentation](https://img.shields.io/badge/-documentation-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/README.md)
|
33 | [![History](https://img.shields.io/badge/-history-blueviolet)](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/history.md)
|
34 |
|
35 |
|
36 | A **lightweight** web server statically **configurable** with regular expressions.
|
37 | It can also be **embedded** and **extended**.
|
38 |
|
39 | # Rational
|
40 |
|
41 | Initially 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 :
|
42 | - A simple web server
|
43 | - A reverse proxy to an existing server
|
44 | - A server that aggregates several sources
|
45 | - ...
|
46 |
|
47 | By 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
|
48 | [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))* to a **handler** that will answer the request.
|
49 |
|
50 | The configuration syntax favors **simplicity** without dropping flexibility.
|
51 |
|
52 | For instance, the definition of a server that **exposes files** of the current directory but **forbids access** to the directory `private` consists in :
|
53 |
|
54 | ```json
|
55 | {
|
56 | "port": 8080,
|
57 | "mappings": [{
|
58 | "match": "^/private/.*",
|
59 | "status": 403
|
60 | }, {
|
61 | "match": "^/(.*)",
|
62 | "file": "./$1"
|
63 | }]
|
64 | }
|
65 | ```
|
66 |
|
67 | # Usage
|
68 |
|
69 | ## npm start
|
70 |
|
71 | * Install the package with `npm install reserve` *(you decide if you want to save it as development dependency or not)*
|
72 | * You may create a start script in `package.json` :
|
73 |
|
74 | ```json
|
75 | {
|
76 | "scripts": {
|
77 | "start": "reserve"
|
78 | }
|
79 | }
|
80 | ```
|
81 |
|
82 | * By default, it will look for a file named `reserve.json` in the current working directory
|
83 | * A configuration file name can be specified using `--config <file name>`, for instance :
|
84 |
|
85 | ```json
|
86 | {
|
87 | "scripts": {
|
88 | "start": "reserve",
|
89 | "start-dev": "reserve --config reserve-dev.json"
|
90 | }
|
91 | }
|
92 | ```
|
93 |
|
94 | ## Global
|
95 |
|
96 | * Install the package with `npm install reserve --global`
|
97 | * Run `reserve`
|
98 | * By default, it will look for a file named `reserve.json` in the current working directory
|
99 | * A configuration file name can be specified using `--config <file name>`
|
100 |
|
101 | **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'`.
|
102 |
|
103 | ## Embedded
|
104 |
|
105 | It is possible to implement the server in an application using the `serve` export :
|
106 |
|
107 | ```javascript
|
108 | const path = require('path')
|
109 | const { check, serve } = require('reserve')
|
110 | check({
|
111 | port: 8080,
|
112 | mappings: [{
|
113 | match: /^\/(.*)/,
|
114 | file: path.join(__dirname, '$1')
|
115 | }]
|
116 | })
|
117 | .then(configuration => {
|
118 | serve(configuration)
|
119 | .on('ready', ({ url }) => {
|
120 | console.log(`Server running at ${url}`)
|
121 | })
|
122 | })
|
123 | ```
|
124 |
|
125 | The resulting object implements the [EventEmitter](https://nodejs.org/api/events.html) class and throws events with parameters, see [Server events](doc/events.md).
|
126 |
|
127 | The package also gives access to the configuration reader :
|
128 |
|
129 | ```javascript
|
130 | const path = require('path')
|
131 | const { read, serve } = require('reserve')
|
132 | read('reserve.json')
|
133 | .then(configuration => {
|
134 | serve(configuration)
|
135 | .on('ready', ({ url }) => {
|
136 | console.log(`Server running at ${url}`)
|
137 | })
|
138 | })
|
139 | ```
|
140 |
|
141 | And a default log output *(verbose mode will dump all redirections)* :
|
142 |
|
143 | ```javascript
|
144 | const path = require('path')
|
145 | const { log, read, serve } = require('reserve')
|
146 | read('reserve.json')
|
147 | .then(configuration => {
|
148 | log(serve(configuration), /*verbose: */ true)
|
149 | })
|
150 | ```
|
151 |
|
152 | **NOTE** : `log` is using [`colors`](https://www.npmjs.com/package/colors) **if installed**.
|
153 |
|
154 | # Complete documentation
|
155 |
|
156 | Go to this [page](https://github.com/ArnaudBuchholz/reserve/tree/master/doc/README.md) to access documentation and articles about REserve.
|
157 |
|