Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

express, and mongoose api post socket hang up

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@gmail.com",
    "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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading