我使用以下代码,当我运行运行此函数的程序时,我收到错误 res is not defiend(TypeError: undefined is not a function),它可能是什么?我在函数参数中拥有它? ?
http.createServer(function (req, res) {
res.redirect("http://localhost:3002");
}).listen(9006);
https://github.com/nodejitsu/node-http-proxy
在那里我使用 使用自定义服务器逻辑设置独立代理服务器
最佳答案
undefined is not a function
表示 redirect
不是 res
的函数(或方法)。我敢打赌,如果您执行 console.log(res)
,您不会收到错误,这意味着,是的,res
已定义,但是 重定向
不是。它是一个 ExpressJS 方法,所以我假设您没有require
'ed Express 是您的应用程序(如果您打算使用它)。
如果您想在不使用 Express 的情况下进行重定向,一种选择是设置不同的位置 header 和响应代码(来自 here ):
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.
编辑
根据您提供的库:
关于javascript - 没有为 res Node 定义错误函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012130/