Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
Error while creating rest API in postman 500

I'm building an API using nodejs for the web I already have a database and the GET request works but when I try POST it gives me This errorError

This is the code

//user 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);
This is the auth.js file for routing

//auth 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);

    }
});

module.exports = router
This is the main index.js file

// index.js
require('dotenv').config()
const express = require("express");
const app = express()
const mongoose = require("mongoose")
const router = require("./routes/auth");
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true})); 

app.use(express.json());
mongoose.connect(process.env.MONGO_URL, ()=>{

    console.log("connected to a db server");

});

app.use("/api/auth", router);


app.listen("5000", ()=>{
    console.log("connected to port 5000.")
});
I see nothing at output but pair of empty curly braces. I have connected it to a mongodb cluster but i see nothing in the database aswell

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try this on your index.js. after your DB is connected. you can start server

// index.js
require('dotenv').config()
const express = require("express");
const app = express()
const mongoose = require("mongoose")
const router = require("./routes/auth");
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.json());
mongoose.connect(process.env.MONGO_URL, () => {

    console.log("connected to a db server");
    app.use("/api/auth", router);


    app.listen("5000", () => {
        console.log("connected to port 5000.")
    });

});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!