0

So I have made a back-end in NodeJS but I ran into one problem, how is it possible to link my back-end to my front end html/css page and use my NodeJS functions as scripts?

1 Answer 1

1

In case this wasn't clear to you, your nodejs back-end runs on your server. The server's job (in a webapp) is to deliver data to the browser. It delivers HTML pages. It delivers resources referenced in those HTML pages such as scripts, images, fonts, style sheets, etc.. It can answer programmatic requests for data also.

The scripts in those web pages run inside the browser which is nearly always (except for some developer testing scenarios) running on a completely different computer on a completely different local network (only connected via some network - usually the internet).

As such, a script in the browser cannot directly reference variables that exist in the server or call functions that exist on the server. They are completely different computers.

The kinds of things you can do in order to work-around this architectural limitation are as follows:

  1. The server can dynamically modify the web page it is sending the browser and it can insert data into that web page. That data can be in the form of already rendered HTML or it can be variables inside of script tags that your web page Javascript can then use.

  2. The javascript in the web page can make network requests to your server asking it for data. These are often called AJAX calls. In this scenario, some Javascript in your page sends a request to the server to retrieve some data or cause some action on the server. The server receives that request, carries out the desired operation and then returns a result back to the client Javascript running in the browser. That client Javascript receives the result and can then act on it, inserting data into the page, making the browser go to a new web page, prompting the user, etc...

There are some other ways that the web page javascript can communicate with the server such as webSocket connections, but we'll put those aside for now as they are just more ways for remote communication to happen - the structure of the communication doesn't really change.

how is it possible to link my back-end to my front end html/css page and use my NodeJS functions as scripts?

You can't directly use your nodejs functions as scripts in the front-end. You can make Ajax calls to the server and ask the server to execute it's own server code on your behalf to carry out some operation or retrieve some data.

If appropriate, you can also insert scripts into the web page and run Javascript directly in the browser, but whether you can do that for your particular situation depends entirely upon what the scripts are doing. If the scripts are accessing some resource that is only available from the server (like a database or a server storage system), then you won't be able to run those types of scripts in the browser. You will have to use ajax calls to ask the server to run them for you and then retrieve the results.

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

2 Comments

thank you for your help, this is a consistent and clear explanation to my problem. However, what is the way one would then make a signup that first sends out the index file unfilled, then once retrieved it executes code on the same server to get a value then send it to a data base? I am using regular html/css and nodejs, if ajax solves this then how would one call a set of instructions to get values from functions from nodejs
@NevadaFlorenzo - The signup data would be in a form in your web page. When the user fills it out and hits "Submit" or "Signup", the browser will send the data in the form to your server in what is called a POST request to a specific URL that is specified in the form. Your server will have to have the appropriate request handler to receive that URL, fetch the data that came from the form and then carry out whatever operations are appropriate on behalf of the user and then finally send back a response which the browser will display. You can read more about forms and posts in your backend.

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.