Node.js - Express - 类型错误 : res. json 不是函数

标签 node.js http express

我正在尝试响应 http 请求,但收到以下错误:

TypeError: req.json is not a function

这是我称之为的地方:

app.get('/', function (req, res) {
const channel_name = req.query.channel_name;
const user_name = req.query.user_name;
const text = req.query.text;

var input = text.split(" ");
if (input[0] == "move") { // Make move

    game.move(user_name, channel_name, input[1], input[2], function(returnGame) {
        req.json(game.getGameStatus(returnGame));
    });
}
else {
    var boardSize = 3;
    if (input.length == 2)
        boardSize = input[1];
    var newGame = game.startGame(channel_name, user_name, input[0], boardSize);
    res.json(game.getGameStatus(newGame));
}
});
app.listen(port);

game.move 和 game.startGame 都返回一个名为 Game 的 JSON。 StartGame 创建 JSON 并返回它,但 move 从数据库加载 JSON 并使用回调返回它。 当我从回调中调用 req.json 时收到错误,但它对于 StartGame 工作正常。

有什么想法吗?我一整天都被困在这个问题上,似乎无法弄清楚。感谢任何帮助,谢谢。

最佳答案

req.json(game.getGameStatus(returnGame)); 处检测到输入错误

.json 是为 res 定义的,而不是为 req 定义的。

关于Node.js - Express - 类型错误 : res. json 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300868/

相关文章:

javascript - AngularJs Web 服务

javascript - 将函数附加到 JavaScript 对象文字的 __proto__ 属性是否是个好主意?

javascript - 检查 DOM 元素的 Node.js 模块

node.js - 在 Node.js 中将 gzip 压缩的 http 请求正文解压缩为 json

json - 使用 crate 的 HTTP API 插入对象数组 : error 4003

java - Play Framework 没有收到大的后期数据

node.js - 警告 : NODE_ENV value of 'test ' did not match any deployment config file names

java - 从 apache HttpResponse 获取响应 Uri 参数

node.js - 将值附加到 res 对象

javascript - NodeJS - socket.on 似乎没有被调用