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);


game.js file in the folder routes:

const express = require('express');

const router = express.Router();

const games = [];

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

});'/create', (req, res, next) => {

module.exports = router;

ejs file for html generating:

<%- include('./includes/head.ejs') %>
    <%- include('./includes/nav.ejs') %>
    <h1>Create a new Game</h1>
        <form action="/game/create" method="POST">
                <input type="text" id="game">
                <button type="submit">start</button>
<%- include('./includes/end.ejs') %>

>Solution :

You need to add name attribute to the input element:

<input type="text" name="game" id="game">

Leave a Reply