Skip to content

Commit df3397d

Browse files
committed
Master publish
1 parent 0cae997 commit df3397d

File tree

32 files changed

+2321
-1
lines changed

32 files changed

+2321
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,106 @@
11
# 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+

admin/app.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var createError = require('http-errors');
2+
var express = require('express');
3+
var path = require('path');
4+
var cookieParser = require('cookie-parser');
5+
var logger = require('morgan');
6+
7+
var indexRouter = require('./routes/index');
8+
var usersRouter = require('./routes/users');
9+
10+
var app = express();
11+
12+
// view engine setup
13+
app.set('views', path.join(__dirname, 'views'));
14+
app.set('view engine', 'ejs');
15+
16+
app.use(logger('dev'));
17+
app.use(express.json());
18+
app.use(express.urlencoded({ extended: false }));
19+
app.use(cookieParser());
20+
app.use(express.static(path.join(__dirname, 'public')));
21+
22+
app.use('/', indexRouter);
23+
app.use('/users', usersRouter);
24+
25+
// catch 404 and forward to error handler
26+
app.use(function(req, res, next) {
27+
next(createError(404));
28+
});
29+
30+
// error handler
31+
app.use(function(err, req, res, next) {
32+
// set locals, only providing error in development
33+
res.locals.message = err.message;
34+
res.locals.error = req.app.get('env') === 'development' ? err : {};
35+
36+
// render the error page
37+
res.status(err.status || 500);
38+
res.render('error');
39+
});
40+
41+
module.exports = app;

admin/bin/www

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('admin:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

0 commit comments

Comments
 (0)