Search code examples
node.jspostkoa

KOA POST method is not sending body


I have a KOA based NodeJS Application, where is straight forward: html + controller

The following is my html

<!DOCTYPE html>
<html>
    <body>
        <form method="POST" action="/admin/login">
            <div>
                <div>
                    Username
                    <input id="username_text" autocorrect="off" autocapitalize="none">
                </div>
                <div>
                    Password
                    <input id="password_text" autocorrect="off" autocapitalize="none" type="password">
                </div>
            </div>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

And on my controller side I have the following code to take username and password:

let fn_loginPost = async function (ctx, next) {
let body = ctx.request.body;
let user_name = body.username_text;
let password = body.password_text;

However, what I have discovered is body object's value is empty {}

I have already used bodyParser in my app.js with correct order:

app.use(bodyParser);
app.use(router.routes());

Please help.


Solution

  • You need name="" attributes on your <input> fields, not id=""