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

231
Vistas
Node.js - Using input parsed from form to route to a specific URL

I am trying to allow a user to enter their name and have it added to the URL.

for example if the users input is John it redirects the user to "/profile/John"

So far I have tried to define a variable name from the parsed input from my html form.

html code;

<h4>Search for your profile by entering your name below.</h4>
<form class="" action="/" method="post">
  <input type="text" name="name" placeholder="Enter your name">
  <button type="submit" name="button">Search</button>
</form>

Node code;

app.get('/',function(req,res){
    var name = String(req.body.name);
});
app.post("/profile/:" + name, (req,res)=>{
  var data = {
    age: 19,
    job: 'Student',
    hobbies: ['Fishing','Video games','Anime','Basketball']
  };

  res.render('profile',{name: req.params.name, data: data});
});

But I get this error;

                     ^
app.get('/profile/:' + name, (req,res)=>{
                       ^


ReferenceError: name is not defined
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you need to put name inside the path quotes, like this

app.get('/profile/:name',(req,res) =>{ }

the : after / indicates that the next value is dynamic

check out ExpressJS - URL Building

about 4 years ago · Juan Pablo Isaza Denunciar

0

app.get('/',function(req,res){
   // var name = String(req.body.name);
});

Above is not necessary part. You should handle like below

app.post("/profile/:name", (req,res)=>{
var name = String(req.params.name)
  var data = {
    age: 19,
    job: 'Student',
    hobbies: ['Fishing','Video games','Anime','Basketball']
  };

  res.render('profile',{name: req.params.name, data: data});
});

You can check the details in this question

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