Object becomes undefined in node.js simple server

Alright, so when I run this server:

var net = require('net');

var server = net.createServer(function (data) {
    console.log("Hello " + data.name);
});

server.listen(1337, '127.0.0.1');

and then browse this page on chrome (from a file, not a domain):

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            var data = new function(){
                this.name = "John";
            };

            $.post('http://127.0.0.1:1337/', data);
        </script>
    </head>
    <body>

    </body>
</html>

I get the following output on my server:

Hello undefinied

My question is, why does my data object's name become undefined when I send it to the server?


Edit: So I changed my server to this:

var http = require('http');

var server = http.createServer(function (data) {
    console.log("Hello " + data.name);
});

server.listen(1337, '127.0.0.1');

and changed my client's script to this:

var data = {
                name: "John"
            };
            $.post('http://127.0.0.1:1337/', data);

But I'm still getting the same error.

net.connection creates a plain TCP socket, not an HTTP server.

You want http.createServer instead, and the function it accepts as its argument doesn't get a simple object containing just the submitted data. You need to read the manual for the http module and see how to process a request object.

There is an example on the node.js homepage (just scroll down).

See also the API docs for request events and node.js Extracting POST data.