0

I am new to React with node Now I want to send data from node js(backend) to React js with response data. Actually, my situation is after signup from Google authentication I want to send that data to a React js (frontend).

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,]
                    }
    });
4
  • You need to go through some tutorials first. Unfortunately, nobody is going to write the complete code for you here. Commented Mar 25, 2019 at 16:39
  • Now I had updated my code, please check sir or give some tutorial links Commented Mar 25, 2019 at 16:40
  • still not enough, you first need to try it. If you get stuck, then someone might be able to help. Commented Mar 25, 2019 at 16:43
  • I stuck in this step the only sir, Now I had fetched data from Google after that I want to display that display for confirmation Commented Mar 25, 2019 at 16:49

1 Answer 1

1

What you are describing composes an issue between computer systems: how to communicate.

Using JSON and REST, you can develop a REST endpoint as a node service.

All a REST endpoint is, is an HTTP Service Adress that behaves in a specific way.

What you need to do, is develop a REST Endpoint within your Node application and call that endpoint using your React application.

You cannot just "Send" the data to a client application, the application has to request it.

If you re-write your call so that your React.JS calls an endpoint, Node.JS authenticates and returns the result back to React, that should work for you.

More information on Node rest endpoints: https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, my doubt if suppose i am using node js as front end and backend means see this code it can send response with datares.render('/examplePage.ejs', {retrievedData : app.get('data')});. Likewise I want for React js
Yes, that is how you would send the data back. You are correct in thinking that their are many ways to do this. But what you need in order to get that result back to the client application (requesting application), is that client application needs to request it from SOME back end. Rather that be node or MVC or .NET, or whatever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.