M
M
Max Mykhailenko2019-11-14 04:12:31
JavaScript
Max Mykhailenko, 2019-11-14 04:12:31

How to transfer data from node.js array to js array?

The task is this: I receive data from the database that I process with node.js, put it into an array, and I need to transfer it to the array in which it is located in the js file. Or put node js data into a file and from there somehow read it into the js array. Can this be done somehow using the local server that I created on node.js?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
profesor08, 2019-11-14
@profesor08

Well, you need your nodejs to set up a web server on some port and return json in the response. In js which in the browser you make a request to your host.

async function getData() {
  const response = await fetch("https:://node-js-host/get");
  const json = await response.json();
}

A
Anton Neverov, 2019-11-14
@TTATPuOT

1. You can create your own API based on Express, for example, which will give the necessary data when requested.
2. If the data is updated infrequently and you know about it - you can save the array as a JSON object to a file and access it from JavaScript.
The bottom line is that you must contact the backend for this array from the frontend.

A
abberati, 2019-11-14
@abberati

Here is a helpful article for your question. While reading, replace php with mode in your head

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question