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

195
Vistas
How do I add functionality to the html I render in express?

So my goal is, right now, to use NodeJS and express together to take the data I get from a form and write it to a local file. The local file is located on my computer i.e. the server (since I am using localhost). Now, I am able to render the form, but how do I add functionality to this form? Because whenever I create an index.js file in the views folder, the html is not able to access it until and until and unless I dont move it to a public folder, which is the static files folder, the html cant access the js file. Now, when I transfer it to the statics folder, require suddenly stops working.

server.js:

const path = require('path')
const app = express()

app.use(express.static(path.join(__dirname, 'public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.get("/", (req, res) => {
    console.log("here")
    res.render("index")
})
app.listen(3000)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you are mixing between the client and server.

The client JS is for adding functionality to you front-end, like, preform some action when a button is clicked. For this you have a few options:

  1. Embedded code - this will be written directly to your HTML inside the <script> tag:
<script>
    document.getElementById("id").innerHTML = "This is my Id";
</script>
  1. Inline code - will be also written to your HTML but not using the <script> tag:
<body id="test">
   <button><a href="#" onClick="alert('Clicked!');">Click Me</a></button>
</body>
  1. External file - a .JS file you will link to your HTML using:
<script src="index.js"></script>

best practice is to place your <script> tag at the very end of your HTML <body> tag.

Now if you want a server to receive the data from the client and preform some actions you will need to create an express server that will expose an endpoint which will do that.

so your HTML will look like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form action="http://localhost:3000/test" method="POST">
      First name: <input type="text" name="firstname" /><br />
      Last name: <input type="text" name="lastname" /><br />
      <button type="submit">Submit</button>
    </form>
  </body>
</html>

And your express JS file will look like this:

const express = require("express");

const app = express();

app.use(express.urlencoded({ extended: true }));

app.post("/test", (req, res) => {
  console.log(req.body);
  res.status(200).send(`Info submitted, Full Name: ${req.body.firstname} Last Name: ${req.body.lastname}`);
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});
about 4 years ago · Juan Pablo Isaza 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