0

I have the following...

var request = require('request');

exports.list = function(req, res){
  res.send("Listing");
};
exports.get = function(req, res){
  request.get("<URL>", function (err, res, body) {
    if (!err) {
      res.send(body,"utf8");
    }
  });
};

This fails with the following....

TypeError: Object #<IncomingMessage> has no method 'send'

How do I do this?

UPDATE tried to use write instead of send but...

/Users/me/Development/htp/routes/property.js:9
  res.setHeader('Content-Type', 'text/html');
      ^
TypeError: Object #<IncomingMessage> has no method 'setHeader'

Also writing out to the console instead works fine.

3
  • Need more information on your query to answer ..... Usage Express framework could be possible solution ..... it makes the work a lot easier Commented Jul 22, 2013 at 2:29
  • What are you using this function for ? res.send is from express API. Are you using that ? Commented Jul 22, 2013 at 5:24
  • I am using express, I will try using the commands for non-express as listed below and see if that works. Commented Jul 22, 2013 at 12:52

3 Answers 3

1

Problem was with scope of variables, my response output was the same name as the response object I got back in my callback. Changing this around (resp vs res) made it work....

exports.get = function(req, res){
  request.get("<url>", function (err, resp, body) {
    if (!err) {
      res.send(body);
    }
  });
};
Sign up to request clarification or add additional context in comments.

2 Comments

are you using express? the response object passed to the http.createServer() callback function, does not have a req.send() method, afaik req.send is added by Express. congrats on solving your problem!
As stated in other comments yes I am using express. Thanks for trying to help!
0

What you are trying to do, is to make Request > Response server. But you are using Request module, that allows to get stuff rather than respond.

What you need is http or better get express.js and use it, as it is straight forward and well popular web framework for exactly what you need.

1 Comment

See my answer it had nothing to do with the framework, I am extremely familiar with express and have used it on multiple projects. The problem was variables hiding each other.
0

I wasn't aware OP is using Express. You will encounter a similar error if you attempt to use req.send with the vanilla HTTP module instead of Express.

var http = require('http');    
function requestHandler(req, res){
  //res.send(200, '<html></html>'); // not a valid method without express
  res.setHeader('Content-Type', 'text/html');
  res.writeHead(200);
  res.end('<html><body>foo bar</body></html>');
};
http.createServer(handler).listen(3000);

1 Comment

You should elaborate more on your answer.

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.