Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created April 29, 2011 05:33
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 aseemk/947895 to your computer and use it in GitHub Desktop.
Save aseemk/947895 to your computer and use it in GitHub Desktop.
Node or Connect bug: form decoding / body parsing
var express = require('express');
var app = express.createServer();
app.set('views', __dirname);
app.set('view engine', 'html');
app.register('.html', require('ejs'));
app.configure(function () {
app.use(express.bodyParser());
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.get('/', function (req, resp) {
resp.render('index.html', {
host: req.headers.host,
url: req.url
});
});
app.post('/', function (req, resp) {
resp.render('success.html', {
data: req.body
});
});
app.listen(8888);
console.log('Visit http://localhost:8888/ in your browser.');
<script>
function bookmarklet() {
var form = document.createElement('form');
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'title';
input.value = document.title;
form.action = 'http://<%= host %><%= url %>';
form.method = 'post';
form.target = '_blank';
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
</script>
<p>Drag this bookmarklet up to your bookmarks bar:</p>
<p>
<script>
document.write('<a href="javascript:(' + bookmarklet + ')();">Test</a>');
</script>
</p>
<p>
Then click it from any of these Amazon pages:
</p>
<ul>
<li><a href="http://www.amazon.com/gp/product/B001U0HAZM">
The Machinist</a></li>
<li><a href="http://www.amazon.com/Family-Guy-Presents-Stewie-Griffin/dp/B000A3DFV8/ref=pd_cp_d_3">
Family Guy Presents Stewie Griffin</a></li>
<li><a href="http://www.amazon.com/gp/product/B000VWYJ86">
The Bourne Ultimatum</a></li>
<li><a href="http://www.amazon.com/gp/product/B000GGSMB2">
Scarface</a></li>
</ul>
<p>Does it work? Or do you see an error page?</p>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Node/Connect form decoding bug?</title>
</head>
<body>
<%- body %>
</body>
</html>
<p>Success! It worked. Here's the data we received:</p>
<pre><%= JSON.stringify(data) %></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment