简体   繁体   中英

How to get data from post form with express.js server

Problem: When the client sumbit form with post reqest to the server, the express server gets an empty body (req.body = {}).

What I am trying to do: To get req.body.username and req.body.password on post reqest from the client (sending the form down below)

Server.js (Main app file)

var express = require('express')
var http = require('http')

const mongoose = require('mongoose')
const { json } = require('express')
var cookieParser = require('cookie-parser')

var app = express()
var server = http.createServer(app)

const bodyParser = require('body-parser')

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const formidable = require('formidable');

mongoose.connect('mongodb://localhost:27017/grabber', {useNewUrlParser: true, useUnifiedTopology: true});

app.use(cookieParser())

app.set("view engine", "ejs")

app.set('views', __dirname+'/views/html/');

app.use("/js", express.static(__dirname + "/views/js"))
app.use("/css", express.static(__dirname + "/views/css"))

const registerRoute = require("./routes/register")
const grabberRoute = require("./routes/grabber")
const downloadRoute = require("./routes/download")
const adminRoute = require("./routes/admin")

app.use("/register", registerRoute)
app.use("/id", grabberRoute)
app.use("/download", downloadRoute)
app.use("/admin", adminRoute)

app.get("/", (req, res) => {
    
    res.redirect("/register")
})


app.use(function (err, req, res, next) {
    res.status(400).send("Error code: 2 <hr> This page is currently inaccessible! <br> <a href='/'>GO TO HOMEPAGE</a>")

    console.log(err)
})



server.listen(80)

The route I have a problem on (On the post request)

const express = require('express')
const router = express.Router()
const kSchema = require("../models/key")
const Schema = require("../models/adminCredentials")
const fs = require('fs');
const formidable = require('formidable');

router.get("/", (req, res) => {


    checkCookie(req.cookies['credentials'], cookieCb)

    function cookieCb(f){

        if(f != null && f != undefined && f !=""){
            res.render("admin", {})
        }else{
            res.render("adminLogin", {})
        }
    }

})

async function checkCookie(cookieV, cb){

    let found = Schema.findOne({Session: cookieV}, function(err,obj){}).then(f => cb(f))
}

router.post("/loginInput", (req, res) => {

    console.log(req.body) 
   
})

module.exports = router 

The html/ejs file I am sending to the client

<html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Login</title>
    </head>

    <body>
        <h1>Login</h1>

        <form action="/admin/loginInput" method="POST">
            <input name="username" id="username" class="username" placeholder="Username">
            <input name="password" id="password" class="password" placeholder="Password">
            <button name="password" type="submit" class="submit">Login</input>
        </form>

        
    </body>

</html>

What I am using:

body-parser - 1.19.0

connect-busboy - 0.0.2

cookie-parser - 1.4.5

crypto-js 4.0.0

ejs 3.1.5

express 4.17.1

mongoose 5.11.19


I searched all day for the solution and then I found out that I didn't gave id to the form.

This is the not fixed:

    <form action="/admin/loginInput" method="POST">
        <input name="username" id="username" class="username" placeholder="Username">
        <input name="password" id="password" class="password" placeholder="Password">
        <button name="password" type="submit" class="submit">Login</input>
    </form>

This is the fixed:

<form id="loginform" action="/admin/loginInput" method="POST">
    <input name="username" id="username" class="username" placeholder="Username">
    <input name="password" id="password" class="password" placeholder="Password">
    <button name="password" type="submit" class="submit">Login</input>
</form>

When I gave id to the form I could acces it from the express server with req.body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM