【发布时间】:2017-07-31 22:02:04
【问题描述】:
所以我在这样的模块中导出回调函数:
(function() {
let request = require("request");
module.exports = function GithubApi(url, callback) {
let options = {
uri: url,
headers: {
"User-Agent": "Me",
"Content-Type": "application/x-www-form-urlencoded"
}
};
request(options, function(err, body) {
let context = {
issues: JSON.parse(body.body).map(function(issue) {
return {
title: issue.title,
comments: issue.comments,
};
})
};
callback(context) // The callback
});
};
}());
当我在我的 GET 请求中使用 express.js 时,这个回调工作得非常好:
app.get("/", (req, res) => {
let url = "some url";
GithubApi(url, (data) => {
res.render("../some-views", data);
});
});
但是当我添加一个socket-emit时,回调函数返回SyntaxError: Unexpected end of JSON input
app.get("/", (req, res) => {
let url = "some url";
GithubApi(url, (data) => {
io.socket.emit("update", {message: data}); // adding this
res.render("../some-views", data);
});
});
无法理解为什么套接字会干扰请求并返回 JSON 错误。有人可以帮忙吗?
【问题讨论】:
-
检查你的 JSON 是否有问题。
标签: javascript json node.js sockets express