Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js has no method 'sendHeader'

Tags:

jquery

node.js

Node.js has no method 'sendHeader'

Installation copy is from git clone git://github.com/joyent/node.git

Is install inside ubunti lucid server as Amazon EC2 AMI file.

The simple say hello via command output is working.

But, when output to browser, it fail to works.

Guys, any idea to fix it?

I hope to use node.js, but seems it is not ready for production usage yet.

ubuntu@ip-10-129-31-63:~/Desktop/node/test3$ node hello_world_server.js
Server running at http://localhost:8080/

/home/ubuntu/Desktop/node/test3/hello_world_server.js:5
    response.sendHeader(200, {"Content-Type": "text/html"});
             ^
TypeError: Object #<ServerResponse> has no method 'sendHeader'
    at Server.<anonymous> (/home/ubuntu/Desktop/node/test3/hello_world_server.js:5:14)
    at Server.emit (events.js:45:17)
    at HTTPParser.onIncoming (http.js:1081:12)
    at HTTPParser.onHeadersComplete (http.js:87:31)
    at Socket.ondata (http.js:980:22)
    at Socket._onReadable (net.js:654:27)
    at IOWatcher.onReadable [as callback] (net.js:156:10)
ubuntu@ip-10-129-31-63:~/Desktop/node/test3$ node -v
v0.5.0-pre

Code to run

var sys = require('sys'),
   http = require('http');

http.createServer(function (req, res) {

  setTimeout(function () {

    res.writeHead(200, {'Content-Type': 'text/html'});

    res.write('<br/><strong>&nbsp;&nbsp;&nbsp;&nbsp;Hello World!</strong>');

    res.end();

  }, 2000);

}).listen(8000);

sys.puts('Server running at http://127.0.0.1:8000/');
like image 324
i need help Avatar asked Mar 02 '11 15:03

i need help


4 Answers

If you are following the NetTuts tutorial, try:

response.writeHead() 

instead of

response.sendHeader

and

response.end() 

instead of

response.close()
like image 76
Esteban Avatar answered Nov 20 '22 18:11

Esteban


The function is probably called writeHead(). "Probably", because I don't know your code and what libraries you load.

http://nodejs.org/docs/v0.4.1/api/http.html#response.writeHead

Hardly no one uses node.js for a complete webserver, but for sub-functionality, e.g. chat server it's already in production use on quite a few sites.

like image 25
Mörre Avatar answered Nov 20 '22 19:11

Mörre


It's "setHeader", not "sendHeader".

And no, node is not ready for production use. That doesn't stop a lot of people from using it, but even they'll tell you that they're a little crazy for doing so.

edit — the other answers may be right, that it may be you're looking for "writeHead()". Since all I'm doing here is looking at the quite lovely node.js documentation at nodejs.org, I'm forced to wonder whether you've done that yet yourself.

like image 2
Pointy Avatar answered Nov 20 '22 20:11

Pointy


I believe the method is writeHead()

like image 2
DannyLane Avatar answered Nov 20 '22 18:11

DannyLane