I’m trying to POST Data to Mongo DB Using Express Js.
I’m new to express just learned for a backend server
This is my app.js
const express = require("express");
const mongoose = require("mongoose");
require("dotenv/config")
const User = require("./model/user.js")
let app = express();
app.use(express.json())
app.get("/", (req,res) => {
res.send("On Home!")
});
app.post("/register", async (req,res) => {
try {
const myUser = new User(req.body);
await myUser.save();
res.send(myUser);
} catch (err){
res.send({error : err});
}
});
mongoose.connect(process.env.DB_URI,(req,res) => {
console.log("Connected to Database!")
});
app.listen("5000" , () =>{
console.log("Listening on localhost 5000")
})
My user.js
const mongoose = require("mongoose");
const User = mongoose.Schema({
name: {
type: String,
required: true,
},
email:{
type:String,
required: true,
},
pass:{
type: String,
required: true,
}
});
module.exports = mongoose.model("user",User)
My Response Is
(I’m Using Postman to test)
Thank You For Your Support