Express req.body is empty


I've tried many StackOverflow answers and the method usually works with body-parser, but I haven't been able to get any output from req.bodyAJAX or form-data .

in server.js:

app.use(helmet()); // Helmet middleware
app.use('/assets', express.static('resources/web/assets')); // Makes /assets public
app.use(require('./resources/modules/session.js')); // Custom session middleware
app.set('view engine', 'ejs'); // Sets EJS to the view engine
app.set('views', `${__dirname}/resources/web/pages`); // Sets the views folder
app.use(cookieParser()); // cookie-parser middleware
app.use(bodyParser.urlencoded({ extended: true })); // body-parser's middleware to handle encoded data
app.use(bodyParser.json()); // body-parser's middleware to handle JSON
app.use(fileUpload({ limits: { fileSize: 100 * 1024 * 1024 } })); // express-fileupload middleware (bushboy wrapper)
app.use('/api', require('./resources/routes/api.js')); // External API router
// ...'/login', (req, res) => {
    res.render('login', {
        page: {
            name: 'Login'
        error: ''

my login.ejscode:

            <form method="POST">
                <div class="input-group">
                    <i class="las la-user"></i>
                    <input placeholder="Username" name="username" type="text" required>
                <div class="input-group">
                    <i class="las la-lock"></i>
                    <input placeholder="Password" name="password" type="password" required>
                <button type="submit">
                    <i class="las la-paper-plane"></i> Login

No matter what I try, I always get {}nothing in the console. I've tried debugging; I need fresh eyes to see what I'm doing wrong. The form data is as follows: I also tried using jQuery's AJAX( ):Tabular data$.get

$.post('', {username:'test', password:'test'})
.done(() => console.log('Success'));

enter image description here

EDIT: After trying multer app.use(require('multer')().array());and app.use(require('multer')().none());middleware, I'm still at the same old problem, except that with multer req.bodyis now undefinednot {}. This is due to data being sent, application/x-www-form-urlencoded not what I thought it was before application/form-data. In this case the body parser middleware method should work . If contributing, please do not contribute answers related to parsingapplication/form-data !

Edit 2: For those asking for session.jsthe code, here it is:

const enmap = require('enmap'),
      sessions = new enmap('sessions');

module.exports = (req, res, next) => {
    if (!req.cookies) next();
    const { cookies: { session: sessionID } } = req;
    if (sessionID) {
        const session = sessions.get(sessionID);
        if (session) {
            req.session = session;
        } else {
            req.session = undefined;
    } else {
        req.session = undefined;

I'm attaching the entire source code because you claim to be able to reproduce it somehow. It can be downloaded from (if it doesn't work please contact me on Discord - PiggyPlex #9993) .


For the specific case you're talking about, usually only the "body-parser" module is needed to access form input fields. A minimal example that I recommend you build on top of is as follows:

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

var app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.get('/login', (req, res) => { /* ... */ });'/login', (req, res) => {
    // ...

So my suggestion is to narrow down the cause of the problem by removing any other middleware other than .NET bodyParser. Try to comment on them one by one and then you will find guilt!

Also note that there is no need to bother trying to make it work with Ajax as it makes no difference. Keep it simple and just try a normal browser submission.

When you find faulty middleware, debug it. If you made it yourself, make sure you don't make any changes to it req.body. If it is a 3rd party middleware then please consult its installation steps very carefully, we are happy to provide further instructions and support.

Edit: some other tips

  • Make sure the request is submitted with the headersContent-Type: application/x-www-form-urlencoded
  • Check for any CORS issues
  • File upload middleware is on a separate path like assets