gpt4 book ai didi

node.js - 我的请求正文在 Node API 中显示为空或未定义

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:50 25 4
gpt4 key购买 nike

这是我用来验证登录的 API,但它没有收到正确的请求正文。

这是我与 MongoCloud 连接的服务器/索引文件。

 const express = require("express");
const mongoose = require("mongoose");
const users = require("./routes/api/users");
const profile = require("./routes/api/profile");
const posts = require("./routes/api/posts");
const app = express();
const bodyParser = require("body-parser");


// DB Config
const db = require("./config/keys").mongoURI;
// Connect Mongo
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("Mongo is connected"))
.catch(err => console.log(err));
// var MongoClient = require("mongodb").MongoClient;


//Enabling body parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

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

app.use("/api/users", users);
app.use("/api/posts", posts);
app.use("/api/profile", profile);

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server is running on port ${port}`));

这是路由文件,其中存在用于注册和登录用户的 API。

    const express = require("express");
const router = express.Router();
const gravatar = require("gravatar");
const bcrypt = require("bcryptjs");

const User = require("../../models/User");
// @Route GET api/users/test
// desc test users route
// access public
router.get("/test", (req, res) => res.json({ msg: "users works" }));

// @Route POST api/users/register
// desc register a user
// access public
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email }).then(user => {
if (user) {
return res.status(400).json({ email: "email already exists" });
} else {
const avatar = gravatar.url(req.body.email, {
s: "200", //size
r: "pg", //rating
d: "mm" //default
});

const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});

// @Route POST api/users/login
// desc Login User/Returning JWT(json web token)
// access public

router.post("/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
//Find user by email

User.findOne({ email }).then(user => {
if (!user) {
return res.status(404).json({ email: "user not found" });
} else {
//Check Password
bcrypt.compare(password, user.password).then(isMatch => {
if (isMatch) {
res.json({ msg: "Success" });
} else {
return res.status(400).json({ password: "Incorrect Password" });
}
});
}
});
});

module.exports = router;

我正在编写一个类似的 API 来注册我的用户,它工作得很好。

最佳答案

我认为您需要导出路由文件,并在 index.js 中使用它,如下所示。

index.js

const express = require("express");
const app = express();
const authRoute = require("./authRoute");

app.use(express.json());

app.use("/auth", authRoute);

const port = process.env.PORT || 3000;
app.listen(port, () => console.log("Listening"));

还有你的路由文件,我称之为 authRoute.js。

const express = require("express");
const router = express.Router();

router.post("/login", async (req, res) => {
console.log(req.body);
res.send(req.body);
});

module.exports = router;

有了这些,如果您向端点/auth/login 发送发布请求,您必须能够读取 req.body。例如,如果使用 localhost:3000:

http://localhost:3000/auth/login

关于node.js - 我的请求正文在 Node API 中显示为空或未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58282649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com