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

98
Visualizações
Node.js/Express.js request body is undefined. body-parser is installed and imported

I am trying to get the text that is typed in by the user in my html input field and push it into an array. But the body of my request ist always empty. I already tried changing the order of code but it is not helping. Where is the Problem? app.js file:

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const gameRouter = require('./routes/game.js');
const siteRouter = require('./routes/site.js');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/game', gameRouter);
app.use('/', siteRouter);

app.listen(3000);

game.js file in the folder routes:

const express = require('express');

const router = express.Router();

const games = [];

router.get('/game', (req, res, next) => {

});

router.post('/create', (req, res, next) => {
    console.log(req.body);
    games.push({game: req.body.game})
    res.redirect('/');
})

module.exports = router;

ejs file for html generating:

<%- include('./includes/head.ejs') %>
</head>
<body>
    <%- include('./includes/nav.ejs') %>
    <h1>Create a new Game</h1>
    
    <main>
        <form action="/game/create" method="POST">
            <div>
                <input type="text" id="game">
                <button type="submit">start</button>
            </div>
        </form>
    </main>
<%- include('./includes/end.ejs') %>
almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to add name attribute to the input element:

<input type="text" name="game" id="game">
almost 4 years ago · Santiago Trujillo Relatório

0

Use this line before app.use():

// For parsing a JSON file
app.use(bodyParser.json())
// For parsing parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
almost 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