-3

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.

CyanPrime
  • 5,096
  • 12
  • 58
  • 79
  • Are you even following the given examples? Quentin gave example: [Scroll to bottom and you will see a sample HTTP server](http://nodejs.org/) – Mustafa Feb 03 '13 at 20:38
  • 1
    What makes you think the server connection callback will automatically give you the object you created and sent on the client? – the system Feb 03 '13 at 20:39
  • @Mustafa What I want is to send a object to the server, and have the server print out a value of the object (in this case the name). I don't want to make text say hello world. – CyanPrime Feb 03 '13 at 20:42
  • Are you aware of HTTP protocol? If you do, Just read the [HTTP docs of NodeJS](http://nodejs.org/api/http.html) – Mustafa Feb 03 '13 at 20:44
  • If you're looking for magic, you won't find it. You need to study the API and write the appropriate code. – the system Feb 03 '13 at 20:47

1 Answers1

3

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.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • `data` *is* a simple object. The anonymous function is being invoked as a constructor with `new`. – the system Feb 03 '13 at 20:35
  • @thesystem — Which makes it an instance of the anonymous constructor function, not a simple object. – Quentin Feb 03 '13 at 20:36
  • Alright, I made the changes (as reflected in my edit), but I'm getting the same error. – CyanPrime Feb 03 '13 at 20:37
  • @thesystem — Although, having tested it, it looks like jQuery can cope with that, so I've deleted the first portion of the answer as it was a red herring. (It is decidedly atypical code though) – Quentin Feb 03 '13 at 20:38
  • @CyanPrime — All you've done is change to using `http` instead of `net`. That's the first step. Read the rest of the answer. – Quentin Feb 03 '13 at 20:39