简体   繁体   English

Express 路由器设置错误:Router.use() 需要中间件 function 但未定义

[英]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.这些请求应该从 SQL 数据库接收和发送数据。

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 Router.use() 需要中间件 function 但得到了一个 undefined

Here is my code:这是我的代码:

This code is from the file dat.js .此代码来自文件dat.js its porpose is just to access the SQL database.它的目的只是访问 SQL 数据库。

import { Sequelize } from "sequelize";

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

This code is from the file: controller.js .此代码来自文件: 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 .此代码位于文件中: 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 .此代码来自文件: 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")
});

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它也在文档中提到: https://expressjs.com/de/guide/routing.html#express-router

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express 路由器:Router.use() 需要中间件 function 但得到了 Object - Express router : Router.use() requires a middleware function but got a Object Router.use()需要中间件功能,但在使用功能时未定义 - Router.use() requires middleware function but got a undefined at function use EXPRESS:Router.use() 需要一个中间件函数,但得到了一个对象 - EXPRESS: Router.use() requires a middleware function but got a Object Router.use() 需要一个中间件函数,但有一个未定义的 - Router.use() requires a middleware function but got a undefined Router.use() 需要中间件功能,但有一个未定义的 - Router.use() requires middleware function but got a undefined TypeError-Router.use()需要中间件功能,但未定义 - TypeError - Router.use() requires a middleware function but got a undefined Router.use() 需要中间件 function 但未定义 - Router.use() requires a middleware function but got undefined express js中的Socket.io:Router.use需要中间件功能,但未定义 - Socket.io in express js : Router.use requires middleware function but got undefined 创建Router.use()的路由时出错,需要中间件功能 - Creating route got error of Router.use() requires middleware function 表达错误:抛出新的TypeError('Router.use()需要中间件功能,但得到了'+ gettype(fn)); - Express error: throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM