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

310
Visualizações
Post data on another page in nodejs

I'm new in nodejs. I'm trying to make login form using ejs engine. What I want is simply print the username and password in the welcome screen using post request. The problem is that if I am using POST action it will print my data but don't display the welcome screen layout and if I used GET action then it will display the screen layout but don't have the data. I have wrriten the code in the app.js file. Is it correct?? I've mentioned my app.js code below for reference. Thanks in advance.

Code:-

app.post('/welcome', function(req, res) {
     res.send('Username is '+req.body.unm+'<br>Password is '+req.body.pwd);
});
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

What issue are your facing? Is the return string not displaying the values? When using template engines you should use generally use render instead of res.send to send the view.

Check the sample s given in ejs home page.

over 4 years ago · Santiago Trujillo Relatório

0

There's some really useful information on the ejs npm page: https://www.npmjs.com/package/ejs

I suggest you look into the express package, which allows you to serve pages, so you would start with something like this:

app.get('/', function(req, res){
  return res.sendFile(__dirname + '/index.html');
});

Here's a useful answer which will help you out: how to implement login auth in node.js

over 4 years ago · Santiago Trujillo Relatório

0

If you are using express with ejs, you need to define the folder that houses your views (.ejs files) like this:

var express = require('express')
app.use(express.static(__dirname + '/views')); // set the static files location for the static html

And instead of res.send, respond with this:

app.post('/welcome', function(req, res) {
        res.render('welcome', {username: req.body.unm, password: req.body.pwd});
});

Construct your html in welcome.ejs and put place holders for <%= username %> and <%= password %>

Sample welcome.ejs file in views directory in project base:

<div class="container">
    <p>Welcome<b><%= username %></b></p>
    <p>Your password is <%= password %> </p>
</div>
over 4 years ago · Santiago Trujillo 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