0

I use the following code and when I run the program which is run this function I got error res is not defiend(TypeError: undefined is not a function),what It can be ?I have it in the function params???

http.createServer(function (req, res) {


    res.redirect("http://localhost:3002");

}).listen(9006);

https://github.com/nodejitsu/node-http-proxy

There I use the Setup a stand-alone proxy server with custom server logic

  • 5
    `res.redirect` is an Express function, not a standard Node function. – loganfsmyth Jun 23 '15 at 19:48
  • @loganfsmyth- thanks but see the documantation which also have res https://github.com/nodejitsu/node-http-proxy –  Jun 23 '15 at 19:51
  • 2
    @Mark What about it? none of that documentation suggests using res.redirect. The error messages says `res.redirect` is undefined, not `res` is undefined. – Kevin B Jun 23 '15 at 19:52
  • 1
    Consider the error. It says the function you tried to call is not a function. You didn't try to call res(), but redirect(), which is a property of res. If res were undefined, that would be a different error. You can use console.log(res) to get a better idea of what is happening. @loganfsmyth is correct. Express adds properties to req and res. – m59 Jun 23 '15 at 19:54
  • @m59-sorry not sure that I got it,how should I use it since I use the following library https://github.com/nodejitsu/node-http-proxy –  Jun 23 '15 at 19:56
  • 1
    There are two answers below that explain how. – Kevin B Jun 23 '15 at 19:56

3 Answers3

3

undefined is not a function means redirect is not a function (or method) of res. I'll bet you if you do console.log(res), you won't get an error, which means that, yes, res is defined, but redirect is not. It is an ExpressJS method, so I assume you haven't require'ed Express is your app, if you were planning to use it.

If you want to redirect without Express, one option is to set a different location header and response code (from here):

response.writeHead(302, {
  'Location': 'your/404/path.html'
  //add other headers here...
});
response.end();

From Wikipedia:

The HTTP response status code 302 Found is a common way of performing URL redirection.

Edit

According to the library you've provided:

output

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • Thanks josh I use the following library,https://github.com/nodejitsu/node-http-proxy how should I use it here ? –  Jun 23 '15 at 19:56
  • @Mark, you're welcome. Same exact way as I just explained it... The library is irrelevant. There is no `redirect` method in your library, so you're out of luck. The example I've provided above shows a way to redirect. – Josh Beam Jun 23 '15 at 19:57
  • @Mark, I edited my answer, see `hostRewrite` in the library. That's one way. However, I'm not sure where you got `redirect` from, as nowhere in that library's API does it mention any `redirect` method. – Josh Beam Jun 23 '15 at 20:00
  • Thanks Josh Did you see my updated post I put the libarary which I use and which logic there,how should I use it with your suggestion (the host rewrite ?I just want that the url redirect the application to the new port of the new server that created....can you assist please? –  Jun 23 '15 at 20:02
  • @Mark, I would recommend just reading through the API, I've never used it before. – Josh Beam Jun 23 '15 at 20:03
0

NodeJs don't have any redirect function use following code for redirect

res.writeHead(302, {
  'Location': 'http://localhost:3002'
  //add other headers here...
});
response.end();

Note TypeError: undefined is not a function means that function you trying to access is not defined.

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • Thanks but I use this https://github.com/nodejitsu/node-http-proxy so function should defined ... –  Jun 23 '15 at 19:54
  • @Mark that module does not add `redirect` method to `res`. – Kevin B Jun 23 '15 at 19:55
  • @KevinB-please see my updated post,I use different library with logic,how should I do this in this case... –  Jun 23 '15 at 20:00
0

You may send a response page using what @Josh wrote or you may also handle the 404 page at the same time with the following code:

var http = require('http'),
    fs = require('fs'),
    util = require('util'),
    url = require('url');

var server = http.createServer(function(req, res) {
    if(url.parse(req.url).pathname == '/') {
        res.writeHead(200, {'content-type': 'text/html'});
        var rs = fs.createReadStream('index.html');
        util.pump(rs, res);
    } else {
        res.writeHead(404, {'content-type': 'text/html'});
        var rs = fs.createReadStream('404.html');
        util.pump(rs, res);
    }
});

server.listen(8080);
Hamed
  • 869
  • 10
  • 26