我不断从 node js express post 请求正文中得到 undefined

I keep getting undefined from node js express post request body

我尝试获取 post req.body 值,但是当我 console.log 我正在使用时,我一直未定义 ("express": "^4.17.1", ) 我在没有 body-parser 和 body-parser 的情况下尝试过,我一直得到相同的结果 我尝试了 Whosebug 中可用的所有解决方案,但我一直得到与您在 server.js

中看到的相同的结果
//jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const _ = require("lodash");
const mongoose = require("mongoose");
const cors = require('cors');

const app = express();
app.use(express.json());
app.use(express.urlencoded({extended:true}));
//app.use(bodyParser.urlencoded({ extended: true }))
//var urlencodedParser = bodyParser.urlencoded({ extended: true })
app.use(express.static("public"));
app.use(cors());


app.get("/", (req, res) => 
{
    res.send("Hello");
});

app.post("/login", async (req, res) => {
    // const username = req.body.username;
    // const  password  = req.body.password;
    const {username , password } = req.body;
    console.log("username : " + username);
    console.log(req.body);
    res.json(username);
});



let port = process.env.PORT;
if (port == null || port == "") {
  port = 5000;
}

app.listen(port, function() {
  console.log("Server has started Successfully at port " + port);
});

这是 post人工请求

localhost:5000/login?username="salem"&password=1234567

并在 package.json

下方吼叫
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Salem",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "lodash": "^4.17.21",
    "mongoose": "^5.13.2"
  }
}

我希望这能让我清楚地了解我的问题

最好的问候 塞勒姆

您正在将数据作为查询参数传递

localhost:5000/login?username="salem"&password=1234567

但从 req.body

获取值

const {username , password } = req.body;

你应该使用的是:

var username = req.query.username;
var password = req.query.password;

您必须将用户详细信息从邮递员发送到正文,或者您可以使用查询对象从 URL.

中检索相同的内容

req.query