0

hi
i have just started learning nodejs. i wrote this simple code

var http = require("http");
var fs=require('fs');
var wbsocket = ('ws');

http.createServer(function(req, res) {

    res.write('2working');

}).listen(8889);

console.log('listing to port 8080');

and run in cmd node filename.js
in cmd doesnt give me any error and when i open webbrowser its continue loading and after 1mint says webpage not avilable.
so please tell me what i have wrong done ?

Edited by phoenix254

2
Contributors
6
Replies
21
Views
2 Years
Discussion Span
Last Post by cereal
1

Ok, does it work now?

Applying the end() method after you write the response body is mandatory. About the headers: if not defined, Node will set implicit headers, but you should always set a status [code|message] response:

By the way, you last attempt wasn't working for you because the send() method does not exists in the http module.

0

is it neccessary to add

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

?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.