While the title may be difficult to understand I hope I can explain it well enough.
Here is my setup: I own the domain www.mysite.com though Godaddy. It uses no-ip to point to a specific ip (lets say 1.2.3.4)
Set up some some subdomains (via A records):
www.dev.mysite.com -- points to 1.2.3.4
www.pi.mysite.com -- points to 1.2.3.4
www.mysite.com -- also points to 1.2.3.4
This setup is all hosted on a raspberry pi running node.js (with express for webserver).
My node file (server.js) to kick everything off looks like this:
var express = require("express"), http = require("http");
var server = express();
// proxy server
http.createServer(function (request, response) {
if (request.headers.host === "mysite") {
console.log("Request made to mysite.com");
res.redirect("http://localhost:5555");
} else if (request.headers.host === "dev.mysite") {
console.log("Request made to dev.mysite.com");
res.redirect("http://localhost:5556");
} else if (request.headers.host === "pi.mysite") {
console.log("Request made to dev.mysite.com");
// DROP ALL TRAFFIC EXCEPT FOR PORT 21
} else {
console.log("Request made to something else");
}
}).listen(80);
process.setuid("pi"); // go back to being a basic user
// main server
server.use(express.static(__dirname + "/main"));
server.listen(5555, function(err) {
console.log("server listening on port 5555");
});
// dev server
server.use(express.static(__dirname + "/dev"));
server.listen(5556, function() {
console.log("server listening on port 5556");
});
So here are my issues that I have. I think they all reside in the above file
server.use() needs to somehow be inside of each server.listen() because each server has its own folder it runs out of
My res.redirect()'s don't work
I have no clue how to drop all traffic except for port 21. The point here was to give me a simple url so I could ssh in. But since pi.mysite.com points to same ip as www.mysite.com, I don't want to deliver my homepage. So I figure I will just drop all traffic to that subdomain.
Notes:
I would also like to mention that I have tried both nginx and http-proxy for the traffic routing. I had issues with both of them and figured I would try this inline method to make it simpler. Turns out it's isn't so simple.
I would like to keep this all in 1 file (server.js) so that I can put "node server.js" into a startup script to run on boot.
I know this isn't the most efficient way to do probably any of this. I am learning. Bear with me
I also know that I should not be running this as root (with sudo). I do it so I can bind my listener on port 80. I use setuid("user") to get rid of this issue (hopefully)
That's all I can think of right now. Please let me know how I can fix and improve this process. I will update the question with more info as needed. Thanks
You should use two different objects for the dev and main server. Here is server.js with the proxy server part snipped:
server.js
var express = require("express"), http = require("http");
var server1 = express();
var server2 = express();
// main server
server1.use(express.static(__dirname + "/main"));
server1.listen(5555, function(err) {
console.log("server listening on port 5555");
});
// dev server
server2.use(express.static(__dirname + "/dev"));
server2.listen(5556, function() {
console.log("server listening on port 5556");
});
As far as the proxy server goes, you are listening with http.createServer, this is not express, so express's res.redirect() function will not be available. Do this instead:
http.createServer(function (request, response) {
if (request.headers.host === "mysite") {
console.log("Request made to mysite.com");
response.writeHead(302, { Location: "http://localhost:5555" });
response.end();
} else if (request.headers.host === "dev.mysite") {
console.log("Request made to dev.mysite.com");
res.redirect("http://localhost:5556");
} else if (request.headers.host === "pi.mysite") {
console.log("Request made to dev.mysite.com");
// DROP ALL TRAFFIC EXCEPT FOR PORT 21
response.end();
} else {
console.log("Request made to something else");
}
}).listen(80);
That's going to send a redirect to your browser. I'm not sure that's what you really wanted.