使用 http 的 express/react/next.js 数据请求未获取任何数据
Getting no data with express/react/next.js data request using http
问题:
我正在尝试使用 http
从 http://localhost:3000/auth/sendUserData 请求数据,但我没有收到 data/response(没有 console.logs)。
我在用什么:
Next.js/React,Node(后台服务器),getInitialProps(Next.js).
代码:
Userdata.js
const http = require("http");
const Userdata = {};
Userdata.getUserData = async function(){
let url = `http://${process.env.HOST}:${process.env.PORT}/auth/sendUserData`
console.log(url);
const options = {
host: process.env.HOST,
port: process.env.PORT,
path: '/auth/sendUserData'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};
export default Userdata;
http://localhost:3000/auth/sendUserData
{
_id: "5c5521f823a5d183945fd62f",
name: "Saddy",
steamID: "76561198151478478",
__v: 0
}
问题出在后端身份验证上,因为它是在服务器上而不是在客户端上呈现的,导致会话丢失
问题:
我正在尝试使用 http
从 http://localhost:3000/auth/sendUserData 请求数据,但我没有收到 data/response(没有 console.logs)。
我在用什么:
Next.js/React,Node(后台服务器),getInitialProps(Next.js).
代码:
Userdata.js
const http = require("http");
const Userdata = {};
Userdata.getUserData = async function(){
let url = `http://${process.env.HOST}:${process.env.PORT}/auth/sendUserData`
console.log(url);
const options = {
host: process.env.HOST,
port: process.env.PORT,
path: '/auth/sendUserData'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};
export default Userdata;
http://localhost:3000/auth/sendUserData
{
_id: "5c5521f823a5d183945fd62f",
name: "Saddy",
steamID: "76561198151478478",
__v: 0
}
问题出在后端身份验证上,因为它是在服务器上而不是在客户端上呈现的,导致会话丢失