1.0.0-beta.1 • Published 7 months ago

@fakehost/utils-app-server v1.0.0-beta.1

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

@fakehost/utils-app-server

Host an existing app with injected runtime variables for running automated tests against.

Run a web server hosting your app with extra variables written into the window object, and with localStorage items set. This can be run to serve either

  • a prebuilt static website
  • to proxy a running development server, e.g. react-scripts start or webpack dev server.

Often an application will be served with some environment variables injected into the index.html page which can then be read by the javascript e.g.

<html>
    <body>
        <!-- content etc -->
        <script>
            window.appEnv = {
                service_url: 'wss://service:54321'
            }
        </script>
    </body>
</html>

Then when your application loads, it will connect to the server using something like:

connectToServer({
    url: window.appEnv
})

Usage

To host a static set of files

import { startAppServer } from '@fakehost/utils/app-server'
const host = startAppServer(
    '../../public/', 
    3010, 
    { appEnv: 'ws://localhost:5555' },
    { 'feature-toggle1': 'true' }
);
// ...
// Static server on http://127.0.0.1:3010 hosting "/full/path/to/public"`
// 
// You can now open http://127.0.0.1:3010 and
// - window.appEnv will be set to 'ws://localhost:5555'
// - localStorage.getItem('feature-toggle1') => 'true'
host.dispose()

To proxy a running dev server

import { startAppServer } from '@fakehost/utils/app-server'
const host = startAppServer(
    3000, 
    3010, 
    { appEnv: 'ws://localhost:5555' }, 
    { 'feature-toggle1': 'true' }
);
// ...
// Proxying http://127.0.0.1:3000 -> http://127.0.0.1:3010
// 
// You can now open http://127.0.0.1:3010 and 
// - window.appEnv will be set to 'ws://localhost:5555'
// - localStorage.getItem('feature-toggle1') => 'true'
host.dispose()

Tip

To run the server on a random available port (e.g. for running tests in parallel against different fake backends), use 0 as the target port:

import { startAppServer } from '@fakehost/utils/app-server'
const host = startAppServer(3000, 0);
console.log(host.port) // -> e.g. 10156
1.0.0-beta.0

9 months ago

1.0.0-beta.1

7 months ago

0.2.0

1 year ago

0.1.1-alpha.3

2 years ago

0.1.1-alpha.6

2 years ago

0.1.1-alpha.5

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago