-
-
Save douyw/4159419 to your computer and use it in GitHub Desktop.
backbone/node.js pushState enabled server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require libraries | |
var os = require("os"); | |
var fs = require("fs"); | |
var readline = require("readline"); | |
var cluster = require("cluster"); | |
var express = require("express"); | |
var site = express.createServer(); | |
// Var up, bro | |
var i, read; | |
var forks = []; | |
var prefix = "server ~ "; | |
// Master thread spawns new listeners | |
if (cluster.isMaster) { | |
function create() { | |
// Spawn a new worker for each available thread | |
for (i = 0; i < os.cpus().length; i++) { | |
forks.push(cluster.fork()); | |
} | |
} | |
function destroy() { | |
// Destroy all original forks | |
forks.forEach(function(fork) { | |
fork.send({ cmd: "kill" }); | |
}); | |
forks = []; | |
} | |
var command; | |
var commands = { | |
"status": function() { | |
forks.forEach(function(fork) { | |
console.log("Worker", fork.pid, fork.online ? "online" : "offline"); | |
}); | |
}, | |
// Destroy all forks and respawn new ones | |
"restart": function() { | |
destroy() && create(); | |
}, | |
// Destroy all forks and kill this master process | |
"stop": function() { | |
destroy(); | |
process.exit(0); | |
} | |
}; | |
// Spin up initial forks | |
create(); | |
// Define prompt | |
read = readline.createInterface(process.stdin, process.stdout); | |
// Wait for data | |
read.on("line", function(line) { | |
command = line.trim() | |
// Detect if command exists | |
if (command in commands) { | |
commands[command](); | |
} | |
// Re-Show the prompt | |
read.setPrompt(prefix, prefix.length); | |
read.prompt(); | |
}) | |
read.on("close", function() { | |
destroy(); | |
process.exit(0); | |
}); | |
// Show prompt | |
read.setPrompt(prefix, prefix.length); | |
read.prompt(); | |
return; | |
} | |
// Kill off the process | |
process.on("message", function(msg) { | |
if (msg.cmd && msg.cmd == "kill") { | |
process.exit(); | |
} | |
}); | |
// Serve static files | |
site.use("/assets", express.static("../client/assets")); | |
site.use("/lib", express.static("../client/lib")); | |
// API serving will happen here | |
// site.use("/api", ...); | |
// Ensure all routes go home, client side app.. | |
site.get("*", function(req, res) { | |
fs.createReadStream("../client/index.html").pipe(res); | |
}); | |
// Actually listen | |
site.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment