【问题标题】:Create my own mvc structure with node.js, express and use restful api用node.js创建自己的mvc结构,表达和使用restful api
【发布时间】:2014-12-28 18:10:16
【问题描述】:

我想要一些 MVC 结构,它是客观的,没有“mimimi”。

我什么也没找到,所以我用restful api创建了我自己的MVC框架。我在为路由传递控制器的数据时遇到了一些麻烦。谁能帮帮我?

我在生成器表达式之上创建,并且是一个分离服务器/客户端的结构。这是迄今为止的结构:

http://i.imgur.com/GKzVSn8.png

注意分离客户端/服务器。

controllers.js:

var example = require('./models/example');

exampleController = function() {

    // POST
    this.create = function(req, res, params) {

    };

    // GET
    this.read = function(req, res, params) {

    };

    // PUT
    this.update = function(req, res, params) {

    };

    // DELETE
    this.delete = function(req, res, params) {

    };

};

module.exports = exampleController;

我可以在这里为某些路线进行渲染吗?

这是我的 client.js 路由:

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

var exampleController = require('../controllers/example');



router.get('/', function(req, res) {

});


module.exports = router;

如何在路由中使用控制器上的功能?

这里有点迷路..

谢谢。

【问题讨论】:

    标签: javascript node.js rest crud


    【解决方案1】:

    为什么不在启动时加载每个控制器,将路由器对象传递给每个控制器:

    使用此模块在启动时加载每个控制器:

    module.exports = function(router) {
        var fs = require('fs');
    
        // Setup controllers
        var controllers = fs.readdirSync(__dirname + '/controllers'); // <-- use your controllers path 
    
        controllers.forEach(function(controllerName){
            var module = require('./controllers/' + controllerName);
    
            if (typeof handle === "function") {
                module(router);
            }
        });
    };
    

    那么exampleController可能是这样的:

    var proto = exampleController.prototype;
    
    function exampleController(router) {
    
        // GET
        router.get('/', proto.index)
    };
    
    proto.index = function(req, res) { ... };
    
    module.exports = exampleController;
    

    记得调用入口点 .js 文件中的第一个模块:

    i.e: server.js:
    
    var controllersLoader = require('path-to-module');
    
    var express = require('express');
    var router = express.Router();
    
    controllersLoader(router);
    

    【讨论】:

    • 我用其他解决方案解决了@Daniel,但你的解决方案也很棒!感谢您的帮助!!!
    【解决方案2】:

    您只需将控制器中的函数用作路由器 get、post 等方法的回调:

    var exampleController = require('../controllers/example');
    
    router.get('/', exampleController.read);
    

    【讨论】:

      【解决方案3】:

      使用属于 Express 的 app.put()、app.post()、app.get() 和 app.delete() 调用。这意味着您必须更改模块的结构才能将 express 应用程序传递给初始化函数来进行设置。

      module.exports = {
          init: function (route, app) {
              app.get(route, function (req, res) {
                  // get functionality
              });
              app.put(route, function (req, res) {
                  // put functionality
              });
              app.delete(route, function (req, res) {
                  // delete functionality
              });
              app.post(route, function (req, res) {
                  // post functionality
              });
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-05
        • 2016-10-05
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多