找到你要的答案

Q:error function is not defined for res node Edit

Q:错误函数未定义为RES节点 编辑

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

我用下面的代码,当我运行,运行这个函数我错误既程序不理解(列表的定义不是一个函数),它可以是什么?我在它的函数参数???

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

answer1: 回答1:

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:

不确定的是不是一个函数重定向是不是一个函数(或方法)与我打赌如果你控制台日志(RES),你不会得到一个错误,这意味着,是的,物的定义,但将不。这是一个expressjs方法,所以我认为你还没有require'ed表达的是你的应用程序,如果你打算使用它。

如果您想重定向而不表达,一个选项是设置一个不同的位置头和响应代码(从这里):

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

从维基百科:

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

编辑

根据你提供的图书馆:

answer2: 回答2:

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);

你会用什么@ Josh写的或者你也可以用下面的代码同时处理404页发送响应页面:

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);
answer3: 回答3:

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.

Nodejs没有重定向功能使用以下代码重定向

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

注意列表的定义不是一个函数,函数你试图访问未定义。

javascript  node.js