Skip to content

Instantly share code, notes, and snippets.

@darobin
Created October 8, 2012 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darobin/3852447 to your computer and use it in GitHub Desktop.
Save darobin/3852447 to your computer and use it in GitHub Desktop.
Simple server that tests delete redirect
var express = require('express')
;
var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
app.use(express["static"](__dirname + "/public"));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.all("/delred", function (req, res, next) {
console.log("Got request = " + req.method);
if (req.method === "DELETE") res.redirect("/delred2");
else res.send({ error: "Method was " + req.method });
});
app.all("/delred2", function (req, res, next) {
console.log("Got request2 = " + req.method);
if (req.method === "DELETE") res.send({ ok: true });
else res.send({ error: "Method was " + req.method + " after redirect" });
});
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
(function () {
function success () {
document.body.bgColor = "#0f0";
}
function error (msg) {
document.body.bgColor = "#f00";
document.getElementById("error").textContent = msg;
}
function runDelRed () {
console.log("running");
var xhr = new XMLHttpRequest();
xhr.open("DELETE", "/delred");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
try {
var res = JSON.parse(xhr.responseText);
if (res.ok) success();
else error(res.error);
}
catch (e) {
error("Exception " + e);
}
}
};
xhr.send();
}
document.getElementById("delred").onclick = runDelRed;
}());
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>DELETE test</title>
</head>
<body>
<p>
Click this to run the test: <button id='delred'>Delete Redirect</button>
</p>
<pre id='error'></pre>
</body>
<script src='delred.js'></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment