Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda