Node.Js error - listener must be a function

I'm trying to create an endpoint / order ... where a POST request can be ordered.

var http = require('http');

var options = {
  hostname: '127.0.0.1'
  ,port: '8080'
  ,path: '/order'
  ,method: 'GET'
  ,headers: { 'Content-Type': 'application/json' }
};

var s  = http.createServer(options, function(req,res) {

  res.on('data', function(){
       // Success message for receiving request. //
       console.log("We have received your request successfully.");
  });
}).listen(8080, '127.0.0.1'); // I understand that options object has already defined this. 

req.on('error', function(e){
  console.log("There is a problem with the request:\n" + e.message);
});

req.end();

      

I get the error "the listener must be a function" .... when I try to run from the command line "node sample.js"

I want to be able to start this service and hang in it. Can a proof someone read my code and give me some basic directions on where I am going wrong? and how can i improve my code.

+3


source to share


1 answer


http.createServer()

does not take an object options

as a parameter. Its only parameter is a listener, which must be a function, not an object.

Here's a really simple example of how it works:



var http = require('http');

// Create an HTTP server
var srv = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('okay');
});

srv.listen(8080, '127.0.0.1');

      

+4


source







All Articles