Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda