flatiron / director

a tiny and isomorphic URL router for JavaScript
http://github.com/flatiron/director
MIT License
5.61k stars 486 forks source link

director eating POST contents #189

Open prophile opened 11 years ago

prophile commented 11 years ago

I'm using director 1.2.0, and apparently this.req.body should have parsed JSON data. However, it seems that it always contains an empty object.

Test case:

var director = require('director');
var http = require('http');

var getRoot = function() {
    this.res.writeHead(200, {'Content-Type': 'text/html'});
    this.res.end("<!DOCTYPE html>\n" +
                 "<html><head><title>Bees</title></head>" +
                 "<body>" +
                 "<script src=\"//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\"></script>" +
                 "<script>Post = function(x) { $.ajax({'type': 'POST', 'url': 'bees', 'data': JSON.stringify(x), 'contentType': 'application/json', 'dataType': 'json'}); };</script>" +
                 "<h1>Test</h1>" +
                 "<script>$(function() { Post('covered in bees'); })</script>" +
                 "</body></html>");
};

var postBees = function() {
    var bees = this.req.body;
    console.log(bees);
    this.res.writeHead(200, {'Content-Type': 'text/plain'});
    this.res.end('Eyes.\n');
};

var router = new director.http.Router({
    '': {'get': getRoot},
    '/bees': {'post': postBees}});

var server = http.createServer(function(request, response) {
    router.dispatch(request, response, function(err) {
        if (err != null) {
           response.writeHead(404);
           response.end();
        }
    });
});

server.listen(8080);
joeybaker commented 10 years ago

Don't JSON.stringify(x) Just pass x as an object.