Call server.js from index.html, client.js

Hello i want to use server.js, (becouse is the main script where i can use packages (node.js))
from client.js or index.html. Somehow i want to call the method from other script or change it so it will work.

  • When user does something in website. it triggers client.js
  • Than it has to trigger server.js and use discord package to send message…

Hey @TGamingStudio if I’m interpreting you correctly I think your best route forward here is to expose some endpoints in your project that you can call from your index.html or client.js.

I put together a super-simple example in my discord bot playground project. Hopefully that’s enough to get you going, and if not let me know where I’m not being clear and we can clean it up.

Happy Glitching!

Also this is just one possible approach, and it might not be the best one depending on exactly what you want to accomplish.

Thanks, but in one you have created there is no html site. I do not want write from server to html but from site (client) to server (bot).

Yep, that was just an example. In your webpage you could place a link to https://{{your-project-name}} (or whatever you call your endpoint) and put the message you want to send in the code for the get() method instead of reading the parameter from the url. Or you could leave it like it is and put the message in the link. You can also use javascript in your client.js file to send a request to that url using jQuery’s $.get() or whatever other client-side framework you’re using.

As I said there are an infinite number of ways to approach this and which ones are “right” depend on the specifics of the problem you’re trying to solve. If you want to share your project name I’d be happy to take a more specific look.

Found it, this works nice, but its triggering by url :confused: I would like to trigger it by Js, so user won’t be able to trigger it.

I don’t know of a way to do that - your client-side code needs to have some way of communicating with your server-side code, and some manner of publicly-accessible endpoint is the only way for the code running in your user’s browser to be able to communicate with the code running on the server in your Glitch project. Essentially you’re building an API for your bot.

You could use something like to hide the communication from the user a little bit, or you could put together some way of authenticating that the request is coming from your website (passing along a token that you match against a value stored in your .env file, for instance). Or if you wanted to go all-out you could implement some sort of authentication using something like Auth0, but even that just adds a layer of misdirection and forces your user (which might also just be your website) to authenticate itself.

But by definition your site’s users will be able to see all of that code in their browser if they want to and can reverse engineer whatever you’re doing to protect your endpoint and access those methods.

Perhaps someone else has a different approach that I’m not thinking of.