Ask coding questions

← Back to all posts
Use require with html
Classfied3D (0)

I have a project in html and I need to use require. I know it is a feature in node.js and javascript is different, but I need some way to use it or run a node.js program from html's javascript.

Answered by R2D2VaderBeef (8) [earned 5 cycles]
View Answer
R2D2VaderBeef (8)

To my knowledge, running node.js within HTML is only possible within desktop app frameworks like NW.js

What you want is to have a backend server in Node.js. Use Express to serve your HTML pages. You can run that code which you need to on the backend server. In the HTML, you can to GET/POST to a certain url in your project when you want to execute the code. On the backend, you can execute the code when that request is made, and send back the output as the response.

Classfied3D (0)

@R2D2VaderBeef I was thinking about that, but I'm using passwords so it may not be the best solution (yes I can encrypt it but it would be harder)

R2D2VaderBeef (8)

@Classfied3D Yeah passwords is always a hard one. Maybe you could use a third party sign-in tool that can just pass you the authenticated user's details.

programmeruser (595)

You can't, unless you create an endpoint that runs the script.