抛出新的TypeError('Router.use()需要一个中间件功能,但是得到了一个'+ gettype(fn))v

时间:2019-03-04 01:54:58

标签: node.js postgresql express routing backend

出现错误:

抛出新的TypeError('Router.use()需要中间件功能,但得到了'+ gettype(fn))v

我是新手,不确定发生了什么!任何帮助将不胜感激,如果有其他有用的信息,请告诉我。提前非常感谢!!!!!!!

这是我的app.js:

const express = require('express');
const app = express(); // object with methods; One method is router; express creates a server; app allows us to access the server; app.listen hooks up our server;
const morgan = require('morgan'); //app.use(...) morgan says console.log(wht app.use does);
const { db, Review, Toilet, User } = require('./server/db/index');

// Logger:
app.use(morgan('dev')); // implies '/' ('/', morgan('dev'));

//Body parser:
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

// public:
app.use(express.static(__dirname + '/public'));

// Backend Routing:
app.use('/api', require('./server/api/index'));

// Setting up server
const PORT = process.env.PORT || 8880;

const syncDb = () => db.sync();

const startListening = () => {
  app.listen(PORT, function() {
    console.log(
      ` ***** I'm leisurely listening on pleasant port of ${PORT} *****`
    );
  });
};

async function bootApp() {
  await syncDb();
  await startListening();
}

bootApp();

这是我的服务器/api/index.js:

const router = require('express').Router();

router.use('/toilets', require('./toiletRoutes'));
router.use('/users', require('./userRoutes'));
router.use('/reviews', require('./reviewRoutes'));

module.exports = router;

这是我的服务器/db/api/toiletRoutes.js:

const express = require('express');
const router = express.Router();
const { Toilet, User, Review } = require('../db/index');

// Get all toilets
router.get('/', async function(req, res, next) {
  console.log('in get all toilets');
  try {
    const allToilet = await Toilet.findAll();
    res.status(200).send(allToilet);
  } catch (error) {
    next(error);
  }
});

module.exports = router;

这是我的package.json:

{
  "name": "newflush",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "engines": {
    "npm": "6.4.1",
    "node": "10.7.0"
  },
  "scripts": {
    "start": "webpack -w & node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.18.0",
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-upgrade": "0.0.23",
    "express": "^4.16.3",
    "google-map-react": "^1.1.2",
    "morgan": "^1.9.1",
    "path": "^0.12.7",
    "pg": "^7.8.1",
    "pg-hstore": "^2.3.2",
    "react": "^16.5.2",
    "react-burger-menu": "^2.5.4",
    "react-dom": "^16.5.2",
    "react-geocode": "^0.1.2",
    "react-google-maps": "^9.4.5",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "redux": "^4.0.0",
    "redux-thunk": "^2.3.0",
    "sequelize": "^4.43.0",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  }
}

Here is my server file tree

2 个答案:

答案 0 :(得分:1)

检查所有出口 应该是:

<img data-src="<?php echo $phone_image_value ?>" class="lazy-load" alt="<?php echo $phone_copy_value ?>">

答案 1 :(得分:0)

您的index.js位于sever/db/api/index.js

并且在您的app.js中,您需要server/api/index.js

将其更改为正确的路径。

更新:

哦,我刚刚注意到了这部分

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

应该是

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

您需要将body-parser模块添加到依赖项中并需要它。