如何使用从节点js到React js的响应数据呈现页面?
我是React with node的新手现在我想将数据从节点js(后端)发送到带有响应数据的React js。实际上,我的情况是在从Google身份验证注册后我想将该数据发送到React js(前端)。
router.get(
'/auth/google/callback',
passportGoogle.authenticate('google', {
failureRedirect: '/',
}),
(req, res) => {
const nameFirst = req.user.profile._json.displayName;
const picture = req.user.profile._json.image.url;
const email = req.user.profile.emails[0].value;
const id = req.user.profile.id;
const user = new User({
user_token: id,
name: nameFirst,
email: email,
picture: picture,
provider: 'Google',
dateSent: Date.now(),
});
User.findOne({ email: email }, (err, docs) => {
if (docs != null) {
// already exist
} else {
// send data `user` with routing [routing to /signupnext,]
}
});
回答如下:
你所描述的是计算机系统之间的一个问题:如何沟通。
使用JSON和REST,您可以将REST端点开发为节点服务。
所有REST端点都是以特定方式运行的HTTP服务地址。
您需要做的是在Node应用程序中开发REST端点并使用React应用程序调用该端点。
您不能只是将数据“发送”到客户端应用程序,应用程序必须请求它。
如果您重新编写调用以便React.JS调用端点,Node.JS会对结果进行身份验证并将结果返回给React,这应该对您有用。
有关Node rest端点的更多信息:https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd