简体   繁体   English

如何在node.js中从module.exports发布数据

[英]How to post data from module.exports in node.js

I am trying to take a user input and save it to a collection in my database. 我正在尝试接受用户输入并将其保存到数据库中的集合中。 I am using Node.js, mongodb, mongoose, express.js and ajax. 我正在使用Node.js,mongodb,mongoose,express.js和ajax。

I am currently trying to take the post when the user submits the form and take the input and save it to my data base from inside of my module.exports in my controller file. 我目前正在尝试在用户提交表单时发布帖子,并接受输入并将其从我的module.exports内部保存到我的数据库中,该文件位于我的控制器文件中。

I was able to make this work when all of the code was in one place inside the server.js but in an attempt to break my code apart appropriately I am trying to separate into a MVC system. 当所有代码都放在server.js内的某个位置时,我能够使此工作正常进行,但是为了适当地分解我的代码,我试图将其分离为MVC系统。

My addProductGroup controller looks like this: 我的addProductGroup控制器如下所示:

//bring in models of data
var groups = require('../models').Groups;
var express = require('express');
var app = express();

//page functions go inside of module.exports
module.exports = {
    index: function(req, res){
        groups.find({}, function(err, groups){
            if(err){
                console.log(err);
            }else{
                res.render('addProductGroup',{title: 'Admin Add Group', adminloggedin: true, subtitle: 'Add a Group', underheaderp: ''});

    app.post('/admin/addProductGroup', function(req,res){
    var newGroupName = req.body.groupname;
    new groupName({
        groupName: req.body.groupname,
    }).save(function(err, doc){
        if(err){
            res.json(err)
        }
        else {
            res.render('addProductGroup',{title: 'Admin ASS Group', adminloggedin: true, subtitle: 'Add a Group', underheaderp: ''});
        }
    });
});
            }
});
    }
}

My controller is getting my data from my groups collection and then rendering my page. 我的控制器正在从我的网上论坛集合中获取数据,然后呈现我的页面。 Then when the user posts the data I am trying to make it take the post data, save it to my database and then render the same exact page. 然后,当用户发布数据时,我试图使其获取发布数据,将其保存到我的数据库中,然后呈现相同的页面。 I have played a lot with the nesting of my functions and order of operations. 我在功能嵌套和操作顺序方面发挥了很多作用。

My groups.js Model : 我的groups.js模型:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var GroupsSchema = new Schema ({
    groupName: String
});

module.exports = mongoose.model('groups', GroupsSchema);
var groupName = module.exports;

I am using a handlebars template for my views. 我正在使用车把模板作为视图。

So is having all of this in my module.exports a possible thing to accomplish? 那么在我的module.exports中拥有所有这些可能完成吗? Do i need to try and write a function outside of my module.exports to make this work? 我是否需要尝试在我的module.exports之外编写函数才能使此工作正常进行? If you need to see any of my other files just let me know. 如果您需要查看其他文件,请告诉我。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

You do not clarify the issue you have, so I will try to provide you some general help: 您不确定您遇到的问题,因此,我将尝试为您提供一些常规帮助:

Concerning organizing your express application you definitly should take a look at the Version 4 introduced Routers . 关于组织快速应用程序,您一定要看一下第4版引入的路由器 You can bind your routes directly on the router object and so seperate your logic into different files (modules). 您可以直接在路由器对象上绑定路由,因此可以将逻辑分成不同的文件(模块)。

Your party.js router could look like: 您的party.js路由器可能如下所示:

var express = require('express').Router()

router.get('/paricipants', function(req, res) {
  res.send('dave','mike')
})

module.exports = router

You can have several such routers (controllers) in your controllers directory (ie /controllers). 您可以在controllers目录(即/ controllers)中拥有多个这样的路由器(controllers)。 Taking full advantage of the module.exports functionality, you may now put an index.js file in this directory, which then will be loaded by default, as you require the directory. 要充分利用module.exports功能,您现在可以将index.js文件放在此目录中,然后根据需要默认加载该文件。

Your index.js file in your controllers directory could look like: 您的controllers目录中的index.js文件如下所示:

var router = require('express').Router()

router.use('/party', require('./party'))

// Your other routes to controllers and eventually global routes here.

module.exports = router

You can then simply require this one module in your app.js: 然后,您可以在app.js中只需要一个模块:

var app = express()

app.use(require('./controllers'))

...

Now just require your mongoose models inside your controllers as you need them. 现在,只需要在控制器中需要猫鼬模型即可。

This will give you a more modular and structured application design. 这将为您提供更加模块化和结构化的应用程序设计。

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM