javascript Nodejs/Expressjs 应用程序结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8428212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:24:56  来源:igfitidea点击:

Nodejs/Expressjs app structure

javascriptnode.jsexpress

提问by georgesamper

Say i have this code to separate routes in expressjs:

假设我有这个代码来分隔 expressjs 中的路由:

module.exports = function(express,app,client) {

    app.get('/', function(req,res,next) {
        var query = 'SELECT * FROM users LIMIT 10';
        var user = client.query(query, function (err, results, fields) {
            res.render('index', {
                title: 'test',
                users: results
            });
            client.end();
        });
    });
}

And require it in app.js:

并要求它app.js

require('./controllers/routes.js')(express,app,client);

1) How do i separate db queries into new files in the best way?

1)如何以最佳方式将数据库查询分离到新文件中?

This file would get pretty big even if i separate db logic.

即使我将数据库逻辑分开,这个文件也会变得非常大。

2) What is a good way to separate routes? Maybe separate modules? and then require them all in app.js?

2)什么是分开路线的好方法?也许单独的模块?然后要求他们都在app.js

回答by alessioalex

There is a similar question here which you should read: How to structure a express.js application?

这里有一个类似的问题,您应该阅读:如何构建 express.js 应用程序?

1) All your query logic should be put in models (modules that reside in /models for example)

1)你所有的查询逻辑都应该放在模型中(例如驻留在 /models 中的模块)

2) Separate all your routes (controllers) into modules (and put them in /routes for ex) By routes I mean for example: - all the logic for "Users" routes go into /routes/users.js

2)将所有路由(控制器)分成模块(并将它们放在 /routes for ex 中) 通过路由我的意思是例如: - “用户”路由的所有逻辑都进入 /routes/users.js

Try to keep you app as MVC-ish as possible.

尽量让你的应用程序像 MVC 一样。

Small example for your app above:

上面你的应用的小例子:

app.js

应用程序.js

// configuration for express etc
require('./routes/index')(app)

routes/index.js

路线/ index.js

var model = require("../models/users.js");

module.exports = function (app) {

  app.get('/', function (req, res, next) {
    model.get_recent(function (err, results) {
      // do stuff with your results
      res.render('index');
    });
  });

}

models/users.js

模型/用户.js

module.exports = {
  get_recent: function(callback) {
    var query = "SELECT * FROM users LIMIT 10";
    database.query(query, callback);
  }
}

回答by Len Xu

In the expressjs download package, there is a folder called "mvc". The author provides a good example for a tiny&efficient mvc structure. Going through the code, you will get much inspiration.

在 expressjs 下载包中,有一个名为“mvc”的文件夹。作者提供了一个小而高效的 mvc 结构的好例子。通过代码,你会得到很多灵感。

回答by Zakiullah Khan Mohamed

How about express-train? i have been using it lately, and it plays well with complex app structures.

如何特快列车?我最近一直在使用它,它可以很好地处理复杂的应用程序结构。