javascript - 没有为 res Node 定义错误函数

标签 javascript node.js

我使用以下代码,当我运行运行此函数的程序时,我收到错误 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.

编辑

根据您提供的库:

output

关于javascript - 没有为 res Node 定义错误函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012130/

相关文章:

javascript - 在 React 中更新状态数组长度

javascript - 为什么进度条不能正常工作?

javascript - JSONLint 说有效的 JSON,但我无法在 JavaScript 中从中获取值

javascript - angularJS:ng-show 不适用于 ng-app?

javascript - Node.js、TypeScript、JavaScript 和 Angular 之间的连接

node.js - 使用变量构建嵌套对象的动态 MongoDB/Mongoose 查询

javascript - 检查 Javascript 中的日期和时间比较

javascript - 亚马逊 s3 : How can I name a new folder after each new username?

linux - NPM 安装 PM2 -g (Ubuntu 14.04 x64) 时出错

javascript - Node.js/Server.js 套接字实现问题