and this is my code (index.js), (auth.js)
const express = require("express");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const authRoute = require("./routes/auth");
const app = express();
dotenv.config();
app.use(express.json());
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}).then(console.log("Connected to MongoDB"))
.catch((err) => console.log("err"));
app.use("/api/auth",authRoute );
app.use("/api/auth",authRoute);
app.listen("3000", () => {
console.log("Backend is running.");
});
const router = require("express").Router();
const User = require("../models/User");
//REGISTER
router.post("/register", async (req, res) => {
try {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
});
const user = await newUser.save();
res.status(200).json(user);
} catch (err) {
res.status(500).json(err);
}
});
//LOGIN
module.exports = router;
and image of the postman result
when I send the request postman posts empty curly brackets. I want to get the post data on the down of the body at the postman
and this is what terminal logs result on the terminal
CodePudding user response:
You should check what receives in user
object at const user = await newUser.save();
data would be in user.data
or use json
instead,
const user = await newUser.save();
res.status(200).jsonp(user);
CodePudding user response:
.env file must be inside the root folder, not the outside