Skip to content

Commit 04c8cd9

Browse files
committed
v3.0.2
1 parent 6934718 commit 04c8cd9

17 files changed

Lines changed: 2338 additions & 2688 deletions

nodejs/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
certs/*
3-
!certs/.gitkeep
3+
!certs/.gitkeep
4+
./config.json

nodejs/README.md

Lines changed: 97 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ Any subdomain of `*.backloop.dev` points to `localhost`!
1313
**Exception:** `backloop.dev`, which points to a page where you can download the certificates.
1414

1515

16-
## Why ?
16+
## Why?
1717

18-
**backloop.dev** solves [mixed-content](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content) issues when developing a WebApp or Backend on local environement while accessing ressources on remote HTTPS sources.
18+
**backloop.dev** solves [mixed-content](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content) issues when developing a WebApp or Backend on local environment while accessing resources on remote HTTPS sources.
1919

20-
The issue is often raised by the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) mechanism that restricts the loading of resources from another origin unless this can be allowed by sending correct [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) headers.
20+
The issue is often raised by the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) mechanism that restricts the loading of resources from another origin unless this can be allowed by sending correct [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) headers.
2121

22-
Which anyway will fall-back on the must-have "non-mixed-content" (No HTTP & HTTPS)
22+
Which anyway will fall back on the must-have "non-mixed-content" (no HTTP & HTTPS).
2323

2424
But making requests to **HTTPS APIs** from **HTTP** sites on **localhost** would not be possible without changing security options on your browser, which is why **backloop.dev** provides SSL certificates with a full loopback domain, to let anyone benefit from a signed certificate on **localhost**.
2525

2626
## Where are the certificates?
2727

28-
Certificates are not bundled with the npm package, but downloaded and updated from [backloop.dev](https://backloop.dev) at installation and runtime, or manually with `backloop.dev-update`. To specify in which directory the certificates should be stored set the environement var `BACKLOOP_DEV_CERTS_DIR`.
28+
Certificates are not bundled with the npm package, but downloaded and updated from [backloop.dev](https://backloop.dev) at installation and runtime, or manually with `backloop.dev-update`. To specify in which directory the certificates should be stored, set the environment variable `BACKLOOP_DEV_CERTS_DIR`.
2929

30-
If the certificates are outdated they are checked and updated at boot.
30+
If the certificates are outdated, they are checked and updated at boot.
3131

3232
## Usage
3333

@@ -42,19 +42,88 @@ Add `-g` to use `backloop.dev` and `backloop.dev-proxy` globally.
4242

4343
(Don't forget to prefix commands with `npx` if not installed globally.)
4444

45-
Start a webserver serving the contents of a directory on `https://whatever.backloop.dev:<port>/`:
45+
#### Static file server
46+
47+
Serve the contents of a directory on `https://whatever.backloop.dev:<port>/`:
4648

4749
```
4850
backloop.dev <path> [<port>]
4951
```
5052

51-
Start a proxy on `https://whatever.backloop.dev:<port>/`
52-
Note: proxy will add `x-forwarded-proto: https` to headers. This is to support express-session and other services and advertise it was served in https.
53+
Example:
54+
```bash
55+
backloop.dev ./dist 4443
56+
# Server started on port 4443 serving files in './dist'
57+
# Open https://myapp.backloop.dev:4443/
58+
```
59+
60+
#### Reverse proxy
61+
62+
Proxy requests from `https://whatever.backloop.dev:<port>/` to a backend.
63+
Supports `http://` and `https://` targets, with optional base path.
64+
Note: adds `x-forwarded-proto: https` to headers for express-session and similar services.
65+
66+
```
67+
backloop.dev-proxy <target> [<port>]
68+
```
69+
70+
Where `<target>` can be:
71+
- `http://host[:port][/path]`
72+
- `https://host[:port][/path]`
73+
- `host[:port]` (legacy format, defaults to http)
74+
75+
Examples:
76+
```bash
77+
# Proxy to a local dev server
78+
backloop.dev-proxy localhost:3000
5379

80+
# Proxy to an https backend with a base path
81+
backloop.dev-proxy https://localhost:8443/api 4443
5482
```
55-
backloop.dev-proxy <target host>[:<target port>] [<port>]
83+
84+
#### Multi-host config mode
85+
86+
Serve multiple hostnames from a single instance, each with its own static files or proxy target:
87+
88+
```
89+
backloop.dev --config=<config.json>
5690
```
5791

92+
Config file format:
93+
```json
94+
{
95+
"port": 7654,
96+
"hostnames": {
97+
"app": { "path": "./dist" },
98+
"api": { "proxy": "http://localhost:3000/v1" },
99+
"admin": { "proxy": "https://anotherwebsite.com:8443" }
100+
}
101+
}
102+
```
103+
104+
This starts a single server on port 7654 where:
105+
- `https://app.backloop.dev:7654/` serves static files from `./dist`
106+
- `https://api.backloop.dev:7654/` proxies to `http://localhost:3000/v1`
107+
- `https://admin.backloop.dev:7654/` proxies to `https://anotherwebsite.com:8443`
108+
109+
Paths are resolved relative to the config file location.
110+
111+
**Path-based routing** is also supported. Use `hostname/path/` keys (trailing slash required) to route different URL prefixes to different handlers on the same hostname:
112+
113+
```json
114+
{
115+
"port": 7654,
116+
"hostnames": {
117+
"tom/static/": { "path": "./public" },
118+
"tom/": { "proxy": "http://localhost:3000" }
119+
}
120+
}
121+
```
122+
123+
Here `https://tom.backloop.dev:7654/static/app.js` serves `./public/app.js`, while `https://tom.backloop.dev:7654/api/users` proxies to `http://localhost:3000/api/users`. The longest matching prefix wins.
124+
125+
#### Certificate update
126+
58127
Manually force update of the certificates:
59128

60129
```
@@ -63,7 +132,7 @@ backloop.dev-update
63132

64133
### Certificate files
65134

66-
You can download the certificates files on [backloop.dev](https://backloop.dev) for your own usage.
135+
You can download the certificate files on [backloop.dev](https://backloop.dev) for your own usage.
67136

68137
### From a node app
69138

@@ -94,7 +163,7 @@ httpsOptionsAsync(function (err, httpsOptions) {
94163
});
95164
```
96165

97-
Or with promises.
166+
Or with promises:
98167

99168
```js
100169
const https = require('https');
@@ -111,7 +180,7 @@ const httpsOptionsPromise = require('backloop.dev').httpsOptionsPromise;
111180
})();
112181
```
113182

114-
The following is not recommended as it will crash your app if certificates are expired. Thus it will refresh them for your next boot ;).
183+
The following is not recommended as it will crash your app if the certificates are expired. It will however refresh them for your next boot ;).
115184

116185
```js
117186
const https = require('https');
@@ -173,23 +242,31 @@ export default defineConfig({
173242
```
174243

175244
Now `npm run dev` will be served on `https://whatever.backloop.dev`
176-
There is a ViteJS plugin that does the very same [vite-plugin-backloop.dev](https://www.npmjs.com/package/vite-plugin-backloop.dev).
245+
There is also a ViteJS plugin that does the same: [vite-plugin-backloop.dev](https://www.npmjs.com/package/vite-plugin-backloop.dev).
177246

178-
## Security
247+
## Security
179248

180249
What if `*.backloop.dev` DNS A and AAAA entries are not pointing to `127.0.0.1` and `::1` but to another IP (malicious ones)?
181-
Then your HTTPS requests will not end-up on your machine, but on this malicious servers.
250+
Then your HTTPS requests will not end up on your machine, but on these malicious servers.
251+
252+
Even if this is very unlikely to happen, you may want to be on the safe side by adding `<what you need>.backloop.dev` in your `/etc/hosts` file.
253+
254+
```
255+
127.0.0.1 localhost whatever.backloop.dev ...
256+
::1 localhost whatever.backloop.dev ...
257+
```
182258

183-
Even, if this is very unlikely to happend, you may want to be on the safe side by adding `<what you need>.backloop.dev` in your `/etc/hosts` file.
259+
## Testing
184260

185261
```
186-
127.0.0.1 localhost whatever.backloop.dev ...
187-
::1 localhost whatever.backloop.dev ...
262+
npm test
188263
```
189264

265+
Uses Node.js built-in test runner (requires Node.js 18+).
266+
190267
## Contributing
191268

192-
`npm run lint` lints the code with [Semi-Standard](https://github.com/standard/semistandard).
269+
`npm run lint` lints the code with [neostandard](https://github.com/neostandard/neostandard).
193270

194271
Pull requests are welcome.
195272

nodejs/bin/update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ const updateAndLoad = require('../src/check').updateAndLoad;
33

44
(async () => {
55
await updateAndLoad(true);
6-
})()
6+
})();

nodejs/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"port": 7654,
3+
"hostnames": {
4+
"tom/static/": { "path": "./" },
5+
"tom/": { "proxy": "https://perki.com" }
6+
}
7+
}

nodejs/eslint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const neostandard = require('neostandard');
2+
3+
module.exports = neostandard({ semi: true });

0 commit comments

Comments
 (0)