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

340
Views
Express.js, get call is returning a 500 error

I created a GET API call intended to fetch every user in my Logins database. For some reason, I keep on getting 500 calls on it. Here is my code:

const http = axios.create({
    baseURL: "http://localhost:8080/api",
    headers: {
        "Content-type": "application/json"
    }
});

function fetchUsers(){
    http.get("/getusers").catch(err => {
        console.log("OOF: " + err.message);
    });
}

fetchUsers();

This is the routes file:

const users = require("../controller/users.controller.js");
var express = require('express');
var router = express.Router();
const pathStart = "/api";

// Retrieve all Users
router.get(pathStart + "/getusers", users.findAll);

My routes file is being used in my app.js file:

var indexRouter = require('./routes');
var userRouter = require('./routes/users.routes.js');

var app = express();

app.use('/', indexRouter);
app.use('/', userRouter);

users.controller.js:

// Retrieve all users from the database.
exports.findAll = (req, res) => {
    console.log("extracting users");
    const user = req.query.user;
    var condition = user ? { user: { [Op.like]: `%${user}%` } } : null;

    Users.findAll({ where: condition })
        .then(data => {
            res.send(data);
        })
        .catch(err => {
            res.status(500).send({
                message: err.message || "Error occurred when retrieving users"
            })
        });
};

This is my output: OOF: Request failed with status code 500

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is because of these two lines :

app.use('/', indexRouter);
app.use('/', userRouter);

Basically you will never enter inside useRouter, the first one catches every request. You need to have something like this :

In app.js

app.use('/api/user/', userRouter);
app.use('/', indexRouter);

In user router

const users = require("../controller/users.controller.js");
var express = require('express');
var router = express.Router();

// const pathStart = "/api"; not needed anymore

// Retrieve all Users
router.get("/getusers", users.findAll);

Your fetch logic :

const http = axios.create({
  baseURL: "http://localhost:8080/api",
  headers: {
    "Content-type": "application/json"
 }
});

function fetchUsers(){
 http.get("/user/getusers").catch(err => {
    console.log("OOF: " + err.message);
 });
}

fetchUsers();
over 4 years ago · Santiago Trujillo 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!