javascript - TypeError : Router. use() 需要中间件函数,但在 Function.use 处未定义

标签 javascript node.js mongodb express

我是 NodeJS 新手,我正在学习如何创建一个连接到 mongoDB 的简单 api 应用程序。

我读了一些博客并研究了一些简单的项目,我开始自己制作,但出现了一个小问题。我遵循了所有步骤,甚至检查了自己解决问题的方法。

但是,我没有成功,当我将 server.js 与 api.js 链接时,问题出现在 server.js 中并抛出以下错误

D:\Node Js\nodeJs\userStory\node_modules\express\lib\router\index.js:458
      throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a undefined
    at Function.use (D:\Node Js\nodeJs\userStory\node_modules\express\lib\router\index.js:458:13)
    at Function.<anonymous> (D:\Node Js\nodeJs\userStory\node_modules\express\lib\application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (D:\Node Js\nodeJs\userStory\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (D:\Node Js\nodeJs\userStory\server.js:26:5)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

如果有人可以看一下并指导我哪里出错了,那将会很有帮助。

//---Server.js
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var config = require('./config');
var mongoose = require('mongoose');


var app = express();

mongoose.connect(config.database,function(err){
	if(err){
		console.log(err);
	}else{
		console.log("Connected to a database");
	}
});


app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(morgan('dev'));



app.use("/api",require('./app/route/api')(app,express));

app.get('*',function(req,res){
	res.sendFile(__dirname + '/public/views/index.html');
});

app.listen(config.port,function(err){
	if(err){
		console.log(err);
	}else{
		console.log("Listening on port "+config.port);
	}
});

//api.js
var user = require('../model/user');

var config = require('../../config');

var secretKey = config.secretKey;

module.exports = function(app,express){

    var api = express.Router();
    //passing the data to the DB

    api.post('/signup',function(req,res){
        var user = new User({
            name:req.body.name,
            username:req.body.username,
            password:req.body.password
        });//body here is the body parser for reading the value from the DB

        //Now saving it on the DB

        user.save(function(err){
            if(err){
                res.send(err);
                return;
            }
            res.json({message:'User has been created'});
        });
    });

};

最佳答案

您应该导出路由器,而不是函数。

var api = express.Router();
//passing the data to the DB

api.post('/signup',function(req,res){
    var user = new User({
        name:req.body.name,
        username:req.body.username,
        password:req.body.password
    });//body here is the body parser for reading the value from the DB

    //Now saving it on the DB

    user.save(function(err){
        if(err){
            res.send(err);
            return;
        }
        res.json({message:'User has been created'});
    });
});
module.exports = api;

相应地更改您的 app.js

app.use("/api",require('./app/route/api'));

关于javascript - TypeError : Router. use() 需要中间件函数,但在 Function.use 处未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48424653/

相关文章:

javascript - 组合倒计时日期和列表按钮过滤器 Javascript

javascript - 宽度为 1px 的表格单元格内的过渡

javascript - 如何在 Javascript 字符串中使用括号

javascript - 使用不断变化的 HashMap 和 Nodejs 写入 Excel

javascript - 如果我有 div 中的数据,有人可以更改发送到服务器的数据吗

javascript - 如何延迟读取带有 node.js 或 javascript 的文件行,而不是非阻塞行为?

javascript - 如何设置一系列setHeader的DRY

node.js - 无法通过docker容器中运行的mongoose访问mongodb

MongoDb 热备份 - 使用 fsyncLock 复制数据/数据库 VS 副本集

linux - 重启mongo进程时mongo服务器问题