0

I am sorry for how I have framed the question in the title but I have started programming very recently so once again, I am really sorry.

I am developing a project with React js as my front-end and node js as my backend, I have been successful in doing some basic test api calls to confirm the connection between the two but now, how am I supposed to actually process different actions. For example, while a user is logging in, I need to first check if they are an existing user or not, sign them in if they are not, deleting a user account, changing username, etc.

I tried very hard to look for relevant articles but all I can find are basic "one-time" api calls, what am I supposed to do for an entire batch of operations? From what I have understood, the process of sending a request from React to getting it processed in Node js is like this:

react js ======> 
(request for operation) node js ======> 
(process the operation) ======> 
send a response back to react

Please correct me if there are any mistakes in my question... Thanks a lot!

3
  • I am not entirely sure what you're asking. How to handle a user login flow? Or how to handle flows in general? First search result in google for login flow: "react login flow": auth0.com/blog/complete-guide-to-react-user-authentication Commented Apr 30, 2021 at 5:54
  • okay so, I looked at this article freecodecamp.org/news/…, it included just this basic api call to present the response on react but lets say, I have a number of such requests to be done FROM react, how do I process those operations? Commented Apr 30, 2021 at 6:01
  • As @jweightman provides a good answer it totally depends on the specific use case(s) Commented Apr 30, 2021 at 6:07

1 Answer 1

1

This question is really broad, so I'm going to focus in on this part at a high level:

I tried very hard to look for relevant articles but all I can find are basic "one-time" api calls, what am I supposed to do for an entire batch of operations?

I'm guessing when you talk about requests and APIs you're talking about HTTP requests and REST APIs, which are fundamentally "stateless" kinds of things; HTTP requests are self-contained, and the backend doesn't have to keep track of any application state between requests in order to speak HTTP.

But backend applications usually do maintain state, often in the form of databases. For example, many REST APIs expose CRUD (create, read, update, and delete) operations for important business entities — in fact, Stack Overflow probably does exactly this for answers. When I post this answer, my browser will probably send some kind of a "create" request, and when I inevitably notice and fix a typo I will send some kind of "update" request. When you load it in your browser, you will send a "read" request which will get the text of the answer from Stack Overflow's database. Stack Overflow's backend keeps track of my answer between the requests, which makes it possible to send it to many users as part of many different requests.

Your frontend will have to do something similar if your project does things which involve multiple interdependent requests — for example, you would need to keep track of whether the user is authenticated or not in order to show them the login screen, and you would need to hold onto their authentication token to send along with subsequent requests.


Edit: Let's walk through a specific example. As a user, I want to be able to create and read notes. Each note should have a unique ID and a string for its text. On the backend, we'll have a set of CRUD endpoints:

var express = require(“express”);
var router = express.Router();

class Note {
    constructor(id, text) {
        this.id = id;
        this.text = text;
    }
}

// This is a quick and dirty database!
notes = {};

router.put("/note/:id", function(req, res) {
    notes[req.params.id] = "hello"; // TODO: accept text as well
    res.send("");
});

router.get(“/note/:id”, function(req, res) {
    res.send(notes[req.params.id].text);
});

module.exports = router;

Then on the client side, one could create a note and then read it like this:

// this request will create the note
fetch("http://localhost:9000/note/42", { method: 'PUT', body: 'hello note!' });

// this request will read the note, but only after it is created!
fetch("http://localhost:9000/note/42")
        .then(res => res.text())
        .then(res => console.log(res));
Sign up to request clarification or add additional context in comments.

4 Comments

exactly so how can I do that with React, make a new file for every action (login/deleting a user) or is there another way to do that? Because from what I have understood from the article I have referred in my comment above, I have to send a request to a file every time, in order to GET data
okay so I should change the endpoint as per my operations, right?
thanks a lot mate, I really appreciate you writing the code to help me out!
Yes, I would recommend a separate endpoint for each operation — a good rule of thumb is to treat endpoints like functions. If my answer and example code helped, please mark it as the accepted answer.

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.