Answer the question
In order to leave comments, you need to log in
How to interact with API on Node.js?
Hello. I'm just starting to learn how to interact with the Node.js API. Worked on several examples of the MERN stack. But now I'm faced with a task that, due to my lack of experience, I don't know how to approach. I need at least a general direction and an example of one request.
So, there is an index.js file where the API is registered, there is also a request test file. My task is to write a simple frontend for interacting with the API: display and create users, display and create time periods for users (for example, to track work on a project).
I want to use pure JavaScript. It is allowed to change the API, but since I still have very little experience in this direction, I plan to do this, if only slightly.
What I don't understand is how to connect index.html and the attached JavaScript file to the existing API. In the training examples with which I worked earlier, Express was used and there I could specify which file to run when the server starts, I have no idea how to do this in my case.
Another point that I encountered for the first time is cold start. Does this only mean a longer wait after the first request, or does it threaten with some other difficulties?
The project lies on https://github.com/khiukv/node-api
I will be grateful for any hints!
Answer the question
In order to leave comments, you need to log in
"there is an index.js file where the API is registered" - if this means that the index.js file contains the server code that implements a certain API, then we assume that this server is deployed at the API_URL address. In this case, to connect Index.html to the API, it is enough that the scripts (in your case, it is frontend.js) on the index.html page access the API_URL. For example, if frontend.js contains the fetch(API_URL + "/users").then(r => r.text()).then(console.log) code, then when the index.html page is loaded, a list of users will be displayed in the console
"In the tutorials with which I worked, Express was used before and there I could specify which file to run when the server starts" - what does this phrase mean? Better show with an example. If I understand everything correctly, then according to your link, the index.js code just implements the server,
Actually, answering the question "how to interact with the API on node.js": just like with any other API - raise the server that implements the API and access it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question