Skip to content

Instantly share code, notes, and snippets.

@tbranyen tbranyen/server.js
Last active Jul 28, 2019

Embed
What would you like to do?
backbone/node.js pushState enabled server
// 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();
// 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.process.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);
@tbranyen

This comment has been minimized.

Copy link
Owner Author

tbranyen commented May 17, 2013

Updated to work with node v0.10.6.

@matewilk

This comment has been minimized.

Copy link

matewilk commented Jun 28, 2015

What if I have more complicated routes? For example domain.com/foo/bar/alpha. Than, the index.html will be served correctly, but all static assets have wrong path. domain.com/foo/bar/main.css which of course doesn't exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.