4N0M41Y
4N0M41Y

Reputation: 342

NodeJS, DOM environment, HTML

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

Answers (1)

ridderhoff
ridderhoff

Reputation: 205

There are at least 3 ways:

1. Move backend code to frontend

If your javascript code can be run in a browser there's actually no reason to use node.js at all.

2. API

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

3. WebSocket

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

Related Questions