spandx mult-host example
18 | 19 |This example demonstrates using spandx to 21 | point at different remote environments based on the current local hostname. 22 |
23 | 24 |25 | For example, visiting localhost:1337 will cause 27 | spandx to proxy to Remote Server 1, and visiting 127.0.0.1:1337 will cause 29 | spandx to proxy to Remote Server 2. 30 |
31 | 32 |Currently remote environment... REMOTE SERVER 1
33 |
34 | Switch to...
35 | 36 |-
37 |
- spandx proxying to Remote Server 1 38 |
- spandx proxying to Remote Server 2 39 |
Using multi-host in your own project
42 | 43 |This example is a very simplified version, using localhost and 127.0.0.1 because they work out of the box. To fully utilize this feature in your own project, please read on.
44 | 45 |46 | This feature is most useful when paired with /etc/hosts to create custom local hostnames. If you work on the website for Foo, Inc, which has these environments: 47 |
48 | 49 |-
50 |
- dev.foo.com 51 |
- qa.foo.com 52 |
- stage.foo.com 53 |
- www.foo.com 54 |
57 | You may want to use spandx to test your JS/CSS/images on each environment before deploying. To do that, you could maintain four spandx config files, which point to each of the four envs. Or, you can utilize mult-host routing. 58 | 59 |
60 | 61 | Create local hostnames for each remote environment by adding this to /etc/hosts: 62 | 63 |dev-local.foo.com 127.0.0.1
64 | qa-local.foo.com 127.0.0.1
65 | stage-local.foo.com 127.0.0.1
66 | www-local.foo.com 127.0.0.1
67 |
68 | Now visiting any of the new hostnames will simply point to localhost. Perfect. Now update your spandx config (only 'host' and 'routes' properties included for clarity).
69 |
70 | Before:
71 |
72 | module.exports = {
73 | host: "localhost",
74 | routes: {
75 | "/": {
76 | host: "http://localhost:8081"
77 | }
78 | }
79 | };
80 |
81 | After:
82 |
83 | module.exports = {
84 | host: {
85 | dev: "dev-local.foo.com",
86 | qa: "qa-local.foo.com"
87 | +---> stage: "stage-local.foo.com"
88 | | production: "wwww-local.foo.com"
89 | | },
90 | | routes: {
91 | | "/": {
92 | | host: {
93 | | dev: "http://dev.foo.com",
94 | | qa: "http://qa.foo.com",
95 | +-----------> stage: "http://stage.foo.com",
96 | production: "http://www.foo.com",
97 | }
98 | }
99 | }
100 | };
101 |
102 | The ASCII arrow is drawn to show how the name "stage" (and similarly dev, 103 | qa, and production), links the local hostname to the remote URL. It can be 104 | read as "If you visit spandx via stage-local.foo.com, then proxy to the 'stage' host, which lives at http://stage.foo.com". 105 |
106 |107 | These names can be a string of your choice, they are not hard-coded. 108 |
109 | 110 |