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

303
Views
Error On Express Router Setup: Router.use() requires middleware function but got a undefined

I will just say first of all that I am aware of almost all the questions asked on this site under this title.

The solutions there were pretty obvious and already done by me (with no success) or only helped for those specific cases and didn’t really work in my case unfortunately.


Now, for the problem:

I'm trying to create a route that will handle a get request and a post request which are sent to the route 'ellipses'. These requests should receive and send data from and to an SQL database.

The problem is that for some reason the router is not ready to get these functions and gives me the error in the title:

Router.use () requires middleware function but got an undefined

Here is my code:

This code is from the file dat.js. its porpose is just to access the SQL database.

import { Sequelize } from "sequelize";

export const sequelize = new Sequelize('TheDataBaseName', 'TheUser', 'ThePassword', {
  host: 'localhost',
  dialect: 'mssql'
});

This code is from the file: controller.js. its porpose is to manage the requests and load the data.

import { sequelize } from "../dat";

export const sendEllipses = async (req, res, next) => {
  try {
    const ellipses = await getEllipsesFromJson();
    return res.send(ellipses);
  } catch (e) {
    console.log(e);
  }
};

export const addNewEllipse = async (req, res, next) => {
  const { body: obj } = req;
  let newEllipse;
  try {
    if (Object.keys(obj) !== null) {
      logger.info(obj);
      newEllipse = await sequelize.query(
        `INSERT INTO [armageddon].[dbo].[ellipses] (${Object.keys(
          obj
        ).toString()})
       values (${Object.values(obj).toString()})`
      );
    } else {
      console.log("the values are null or are empty");
    }

    return res.send(newEllipse);
  } catch (error) {
    console.log(error);
  }
};

This code is on the file: routers.js. its porpose is to define the route

import Router from "express";
import { sendEllipses } from "../ellipses.controller";
import { addNewEllipse } from "../ellipses.controller";

const router = Router();
export default router.route("/ellipses").get(sendEllipses).post(addNewEllipse);

This code is from the file: app.js. This is where everything actually happens.

import { router } from "../routers";
import express from "express";

app.use('/api', router);

app.listen(5000, () => {
  console.log("server is runing on port 5000")
});

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

0

You need to export the router

const router = Router();
router.route("/ellipses").get(sendEllipses).post(addNewEllipse)
export default router

Now import the router:

import routes from "../router.js";
app.use('/api', routes);

Its also mentioned in the docs: https://expressjs.com/de/guide/routing.html#express-router

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!