|
1 | 1 | # ExpressVirtualHostExample |
2 | | -A simple example of a virtual host setup using NodeJS and Express |
| 2 | + |
| 3 | +A simple example of a virtual host setup using NodeJS and Express applications. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +Virtual Hosts allow you to handle multiple domain names through a single server instance compared to having to run a separate server per host configuration. This will share **all system resources** such as memory, processor cycles, and storage across all your applications. |
| 8 | + |
| 9 | +[ExpressJS](https://expressjs.com/) is a minimal and flexible web application framework that runs ontop of [Node.js](https://nodejs.org/en/). Express allows for both dynamic and static website hosting and is ideal for this scenario. |
| 10 | + |
| 11 | +### Prerequisites |
| 12 | + |
| 13 | +1 -- Install [Node.js and npm](https://nodejs.org/en/download/) if they are not already installed on your machine. |
| 14 | +> Verify that you are running at least node 6.9.x and npm 3.x.x by running `node -v` and `npm -v` in a terminal/console window. Older versions produce errors, but newer versions are fine. |
| 15 | +
|
| 16 | +2 -- Optionally, install `Git` if you do not have it already installed on your machine. If you do not install Git, you will have to download this repository separately as a zipped folder and unzip it later. |
| 17 | + |
| 18 | +3 -- Have ownership over a website(s) and ability to modify the DNS/Network configuration for each host. |
| 19 | + |
| 20 | +4 -- Create `A` DNS records for each host you want your server instance to manage and have it set to the IP Address of your sever. |
| 21 | + |
| 22 | +### Installing |
| 23 | + |
| 24 | +1 -- Clone this repository onto your server instance using `Git`. If you do not have Git installed, you will need to download this repository as a folder and unzip it. |
| 25 | + |
| 26 | +```sh |
| 27 | +git clone https://github.com/Manbearpixel/ExpressVirtualHostExample.git |
| 28 | +``` |
| 29 | + |
| 30 | +2 -- In your terminal/console window install the dependencies this project requires. |
| 31 | + |
| 32 | +```sh |
| 33 | +# switch to the downloaded repository |
| 34 | +cd ExpressVirtualHostExample |
| 35 | + |
| 36 | +# install dependencies |
| 37 | +npm install |
| 38 | +``` |
| 39 | + |
| 40 | +3 -- If you are going to use the example express application sub directories (admin, dash, and proto) install their dependencies as well. |
| 41 | + |
| 42 | +```sh |
| 43 | +# install sub-folder dependencies (this includes: admin/, dash/, proto/ |
| 44 | +npm init |
| 45 | +``` |
| 46 | + |
| 47 | +4 -- You will need to edit `boot.js` properly by pointing your domain names (example.com) or subdomain names (sub.example.com) to the respective folder. You can use the current example folders to play with for now (admin, dash, proto). |
| 48 | + |
| 49 | +```js |
| 50 | +// This function creates a virtual host for a specified "domainName" |
| 51 | +// and will send traffic to the "directoryPath" |
| 52 | +function createVirtualHost(domainName, directoryPath) |
| 53 | +... |
| 54 | + |
| 55 | +// This creates an instance of a virtual host and will route traffic |
| 56 | +// that hits "dash.example.com" to the folder "dash/" |
| 57 | +let dashboardHost = createVirtualHost('dash.loki.chat', 'dash'); |
| 58 | +... |
| 59 | + |
| 60 | +// This tells the primary Express App to use the virtual host instance |
| 61 | +// created earlier |
| 62 | +app.use(dashboardHost); |
| 63 | +``` |
| 64 | + |
| 65 | +### Running |
| 66 | + |
| 67 | +To run the Express VirtualHost server, simply run the command `npm run start`. This will listen to traffic hitting your server (default port = :80). Assuming you have the proper DNS settings for your websites and matching domains set in `boot.js` then traffic will flow properly. You can turn off your server by stopping this process. |
| 68 | + |
| 69 | +If you want to run your server indefinitvely (until you stop it later) and have this server process run in the background, you can install `forever` via npm and execute the `boot.js` file. |
| 70 | + |
| 71 | +``` |
| 72 | +# install foreverjs globally |
| 73 | +npm install forever -g |
| 74 | + |
| 75 | +# execute boot.js "forever" ...until you manually stop it |
| 76 | +forever start boot.js |
| 77 | + |
| 78 | +# lists all processes managed by foreverjs |
| 79 | +forever list |
| 80 | + |
| 81 | +# stop a specific forever process, replace 0 with the proper number |
| 82 | +forever stop 0 |
| 83 | +``` |
| 84 | + |
| 85 | +## Built With |
| 86 | + |
| 87 | +* [Node.js](https://nodejs.org/en/) - The JavaScript runtime engine |
| 88 | +* [Express](https://expressjs.com/) - Web application framework |
| 89 | +* [Vhost](https://github.com/expressjs/vhost) - VirtualHost middleware for Express |
| 90 | + |
| 91 | +## Contributing |
| 92 | + |
| 93 | +No contributions at this time. |
| 94 | + |
| 95 | +## Versioning |
| 96 | + |
| 97 | +Using [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags). |
| 98 | + |
| 99 | +## Authors |
| 100 | + |
| 101 | +* **Manbearpixel** - *Initial work* - [Manbearpixel](https://github.com/Manbearpixel) |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
| 106 | + |
0 commit comments