Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Accessing API handling node in express ejs front end

I've set up a working, very simple express server with ejs as my view engine.

My web page has a form, and with the press of an html button the input is turned into the json object userFormData. I have another node.js file that handles interactions with an API. On this file, I have a function createNewFromUserFormData(userFormData). I now wonder what the best way would be to get my userFormData from the front end, to the API handling js node on the back end. Is there any way I can call a function on the node from the frontend index.ejs in the view folder?

Basically my question is just: How can I export json from the webapp to a node server? Is there any way I can call a function on the backend from a onclick on the index.ejs?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

EJS (Embedded JavaScript) is a template engine using vanilla JavaScript which will result in static HTML document (or partials).

Handling any events (Network, user...) would go into the HTML document embedded scripts.

You should be able to attach an event handler as the onclick function calling your backend API (with whatever content you should send along as request payload).

Below is an example of a simple EJS template file using vanilla JavaScript to register a click event handler which fetches a remote API (which should be your internal route endpoint) to display its result:

<span id='link'>
    <% if (true) { %>
        <%= 'Guess what to do?' %>
      <% } %>
</span>
<p id="activity"></p>

<script>
    const linkNode = document.querySelector('#link');
    linkNode.addEventListener('click', () => {
        fetch('https://www.boredapi.com/api/activity')
            .then((res) => res.json())
            .then((res) => {
                const textNode = document.querySelector('#activity');
                textNode.textContent = res.activity;
            });
    });
</script>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda