TypeError:app.use()需要中间件功能 [英] TypeError: app.use() requires middleware functions

查看:38
本文介绍了TypeError:app.use()需要中间件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的server.js文件和api.js文件.我在尝试根据其属性搜索js对象的排序函数中遇到错误.事件模式具有名称,位置,价格,等级等属性.我试图根据他们的价格对其进行排序.

This is my server.js file and api.js file. I am getting an error in the sort function in which I intend to search the js objects according to their attributes.The event Schema has the attributes as name, location, price, rating. I tried to sort it according to their prices.

server.js

var express= require('express');
var bodyParser= require('body-parser');
var morgan = require('morgan');
var config=require('./config');
var app= express();
var mongoose=require('mongoose');
//var User=require('./database/user')
mongoose.connect('mongodb://localhost:27017/db',function(err){
    if(err){
        console.log(err);
    }
    else{
        console.log("connected!");
    }
});

app.use(bodyParser.urlencoded({extended: true })); //if false then parse only strings
app.use(bodyParser.json());
app.use(morgan('dev'));//log all the requests to the console
var api=require('./app/routes/api')(app,express);
app.use('/api',api);
app.get('*',function(req,res){
    res.sendFile(__dirname + '/public/views/index.html');
});   // * means any route

app.listen(config.port,function(err){
    if(err){enter code here
        console.log(err);
    }
    else{
        console.log("The server is running");
    }
});
module.exports = router;

api.js

var User = require('../models/user');
var Event = require('../models/event');
var config = require('../../config');
var secret = config.secretKey;

module.exports = function (app, express) {
    var api = express.Router();
    app.use()

    api.post('/signup', function (req, res) {
        var user = new User({
            name: req.body.name,
            username: req.body.username,
            password: req.body.password
        });
        user.save(function (err) {
            if (err) {
                res.send(err);
                return;
            }
            res.json({
                message: 'User created!'
            });
        });

    });

    api.get('/users', function (req, res) {
        User.find({}, function (err, users) {
            if (err) {
                res.send(err);
                return;
            }
            res.json(users);
        });
    });

    api.post('/eventfeed', function (req, res) {
        var event = new Event({
            name: req.body.name,
            location: req.body.location,
            description: req.body.description,
            price: req.body.price,
            rating: req.body.rating
        });

        event.save(function (err) {
            if (err) {
                res.send(err);
                return;
            }
            res.json({
                message: 'Event created!'
            });
        });
    });

    // utility function for sorting an array by a key in alpha order
    api.get('/sortby_price', function (err) {
        if (err) return err;
            // utility function for sorting an array by a key in parsed numeric order
        else {
            function sortArrayNum(arr, key) {
                arr.sort(function (a, b) {
                    return parseInt(a[key], 10) - parseInt(b[key], 10);
                });
            }

            var dicts = EventSchema.saved;
            for (var i = 0; i < dicts.length; i++) {
                var terms = dicts[i].terms;
                sortArrayNum(terms, "price");
            }
        }
        return api;
    });
}

这是我的错误.我是第一次使用此网页.请帮我这个错误告诉你什么.

This is my error. I am making a webpage for the first time using this. Kindly help me what does this error tells.

TypeError:app.use()需要中间件功能
在EventEmitter.use(c:\ Users \ MY APY \ WebstormProjects \ Main \ node_modules \ express \ lib \ application.js:209:11)
在module.exports(c:\ Users \ MY LAPY \ WebstormProjects \ Main \ app \ routes \ api.js:10:9)
在对象.(c:\ Users \ MY LAPY \ WebstormProjects \ Main \ server.js:20:36)
在Module._compile(module.js:460:26)
在Object.Module._extensions..js(module.js:478:10)
在Module.load(module.js:355:32)
在Function.Module._load(module.js:310:12)
在Function.Module.runMain(module.js:501:10)
在启动时(node.js:129:16)
在node.js:814:3

TypeError: app.use() requires middleware functions
at EventEmitter.use (c:\Users\MY APY\WebstormProjects\Main\node_modules\express\lib\application.js:209:11)
at module.exports (c:\Users\MY LAPY\WebstormProjects\Main\app\routes\api.js:10:9)
at Object. (c:\Users\MY LAPY\WebstormProjects\Main\server.js:20:36)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

推荐答案

我被排除在外时遇到了这个问题

I had this problem when I left out

module.exports = router;

在我的Routes.js中.我们需要导出所有路由.

in my Routes.js.We need to export all the routes.

在我的server.js中,

In my server.js, I had

var mainRoutes = require('./Routes.js')
app.use(mainRoutes)

因此,请检查您的"app/routes/api"文件,查看其是否具有正确的导出功能.

So check your 'app/routes/api' file to see if it has proper export.

这篇关于TypeError:app.use()需要中间件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆