3

I am a beginner to node.js and i did a sample code it shown below,

  var http = require("http");
  var server = http.createServer(function(request,response) {
   response.writeHead(200, {
    "content-Type" : "text/html"
   });
  response.end("Hello again");
 }).listen(8888);

and when i run this file on eclise Run as ------> Node project and when i open the browser with url localhost:8888 it shows web page not availble. can u guys help me to find out. I already installed node.js on my system and npm alse. am i missing something?

2
  • ` }).listent(8888);` I think you mean listen Commented Sep 7, 2016 at 9:31
  • @N.J.Dawson i corrected that mistake again it show web page not available Commented Sep 7, 2016 at 9:33

3 Answers 3

2

There is no request or response object in the scope of your request callback. You need to define them as arguments of the callback function.

var http = require("http");
var server = http.createServer(function(request, response) {
  response.writeHead(200, {
    "content-Type" : "text/html"
  });
  response.end("Hello again");
}).listen(8888);

You should definitely get an error though - are you sure your IDE is set up properly?

Sign up to request clarification or add additional context in comments.

1 Comment

i just installed enide plugin on eclipse
0

You never accept the "request" variable. Below is a working version of what you're attempting.

var http = require("http");
var server = http.createServer();

server.on('request', function(request, response) {
   response.writeHead(200, {
    "content-Type" : "text/html"
   });
  response.end("Hello again");
});

server.listen(8888);

7 Comments

Except he does: check the docs for createServer. The first argument can be the request handler.
@Gant I did check the docs, but missed that, he doesn't define the param to receive "request" is probably more accurate then
@Gant not sure if you're being sarcastic or not, you seem to be a very curt fellow. If the edit makes the content makes sense, I have no idea what the problem is with it being "minimalistic"
@N.J.Dawson i tried your code it shows web page not available
@user3189828 running just that code & accessing localhost:8888 returns a webpage with "Hello again" for me - as Gant said, have you got it set up correctly in terms of development environment?
|
0

Can you please tell me where you found response object? http.createServer return a callback function which have two arguments. They are response and request. response use for send data/information to client and request use for get data/information from client. So in your http.createServer callback function add response and request arguments. After that in callback function use response object. Like this.

var http = require("http");
var server = http.createServer(function(request, response) {
    response.writeHead(200, {
        "content-Type" : "text/html"
    });
    response.end("Hello again");
 }).listen(8888);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.