Reputation: 342
I was wondering if there is a way to implement NodeJS Back-End to a Front-End DOM environment. I have an application written in NodeJS but I want the users to be able to connect to a regular html page with a responsive GUI on my hosting solution, for that I am going to need to access my functions from NodeJS files and reference it as a script for the Front-End html. Is this possible? Thanks!
Upvotes: 1
Views: 223
Reputation: 205
There are at least 3 ways:
If your javascript code can be run in a browser there's actually no reason to use node.js at all.
The standard way to do this is to have a node.js backend which works as a webserver (ie: expressjs), providing a REST API that interfaces with the node.js functions you want to be able to access on frontend. To interact with the api on the frontend you can use fetch
or XMLHttpRequest
. A bonus to this method is if it's applicable you can run the backend on a remote host with minor changes
If you have data that has to be near real-time and/or changes frequently a websocket might be the best route. You can install a package like ws on the backend and use the builtin WebSocket API on frontend.
Upvotes: 3