I'm sending a post req via Postman to localhost:5000/api/auth/register, and the socket keeps hanging up on me. What am I missing? Thanks in advance! (I'm using mongoDB's free cluster)
{
"username": "ChronoKross",
"email": "[email protected]",
"password": "123"
}
here is my schema:
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
username:{
type: String,
required: true,
unique: true
},
email:{
type: String,
required: true,
unique: true
},
password:{
type: String,
required: true
},
profilePicture:{
type: String,
default: "",
},
},
{ timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
here is my route:
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 here is my index.js:
const express = require('express');
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const authRoute = require("./routes/auth");
dotenv.config();
app.use(express.json);
mongoose
.connect(process.env.MONGO_URL)
.then(console.log("Connected to MONGODB"))
.catch(err=>console.log(err));
app.use("/api/auth", authRoute);
app.listen("5000", () => {
console.log("Backend is running.");
})
Im following an online guide, and I can't find anything that I have done wrong. I'm using NodeJs, ExpressJs, MongooseJs, and Postman. If you need any additional information please let me know. -- I was just about to embarrassed to post this question lol...
CodePudding user response:
express.json
is a function, also try to start the server after the DB has successfully connected:
app.use(express.json());
app.use("/api/auth", authRoute);
mongoose
.connect(process.env.MONGO_URL)
.then(() => {
console.log("Connected to MONGODB")
app.listen("5000", () => {
console.log("Backend is running.");
})
})
.catch(err=>console.log(err));