Skip to content

Commit

Permalink
The file endpoint no longer resolves paths.
Browse files Browse the repository at this point in the history
This allows it to be used outside of lib/cube; for example, you can now add a
static file endpoint to your visualizer if you want to host custom pages.
  • Loading branch information
mbostock committed Oct 5, 2011
1 parent f394dfb commit 89875d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
6 changes: 1 addition & 5 deletions lib/cube/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function re(re, dispatch) {
}

function file() {
var files = Array.prototype.map.call(arguments, resolve),
var files = Array.prototype.slice.call(arguments),
type = types[files[0].substring(files[0].lastIndexOf(".") + 1)];
return function(request, response) {
var modified = -Infinity,
Expand Down Expand Up @@ -78,10 +78,6 @@ function file() {
};
};

function resolve(name) {
return path.join(__dirname, name);
}

function fiveohoh(request, response) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.end("500 Server Error");
Expand Down
37 changes: 21 additions & 16 deletions lib/cube/server/visualizer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var url = require("url"),
path = require("path"),
endpoint = require("./endpoint");

exports.register = function(db, endpoints) {
Expand All @@ -9,23 +10,23 @@ exports.register = function(db, endpoints) {
endpoint.exact("/", createBoard(db)),
endpoint.re(/^\/[0-9][0-9a-z]{5}(\/edit)?$/, loadBoard(db)),
endpoint.exact("/cube.js", endpoint.file(
"../client/start.js",
"../client/cube.js",
"../client/piece.js",
"../client/piece-area.js",
"../client/piece-sum.js",
"../client/piece-text.js",
"../client/palette.js",
"../client/squares.js",
"../client/board.js",
"../client/header.js",
"../client/end.js"
resolve("start.js"),
resolve("cube.js"),
resolve("piece.js"),
resolve("piece-area.js"),
resolve("piece-sum.js"),
resolve("piece-text.js"),
resolve("palette.js"),
resolve("squares.js"),
resolve("board.js"),
resolve("header.js"),
resolve("end.js")
)),
endpoint.exact("/cube.css", endpoint.file(
"../client/body.css",
"../client/palette.css",
"../client/board.css",
"../client/piece.css"
resolve("body.css"),
resolve("palette.css"),
resolve("board.css"),
resolve("piece.css")
)),
endpoint.exact("/d3/d3.js", endpoint.file(
"../../../node_modules/d3/d3.min.js",
Expand Down Expand Up @@ -61,7 +62,7 @@ function createBoard(db) {

function loadBoard(db) {
var boards,
file = endpoint.file("../client/visualizer.html");
file = endpoint.file(resolve("visualizer.html"));

db.collection("boards", function(error, collection) {
boards = collection;
Expand Down Expand Up @@ -173,6 +174,10 @@ function viewBoard(db) {
return dispatch;
}

function resolve(file) {
return path.join(__dirname, "../client", file);
}

function emit(callbacks, event) {
callbacks.forEach(function(callback) {
callback(event);
Expand Down
5 changes: 3 additions & 2 deletions test/endpoint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ var vows = require("vows"),

var suite = vows.describe("endpoint");

var port = ++test.port,
server = http.createServer(endpoint.file("../client/semicolon.js", "../client/semicolon.js"));
var file = "lib/cube/client/semicolon.js",
port = ++test.port,
server = http.createServer(endpoint.file(file, file));

server.listen(port, "127.0.0.1");

Expand Down

0 comments on commit 89875d1

Please sign in to comment.