Nodejs / Expressjs应用程序结构 [英] Nodejs/Expressjs app structure

查看:86
本文介绍了Nodejs / 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();
});
});
}

并在 app.js

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

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



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



2)什么是分离路由的好方法?也许单独的模块?然后要求它们全部在 app.js

解决方案

您应该阅读一个类似的问题:如何结构一个express.js应用程序?



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



2)将所有路由(控制器)分为模块(并将其放在/路由中)
通过路由我的意思是:
- 所有


$ p
$ b b $ b

上面的应用程序的小例子:



app.js

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

routes / index.js

  var model = require(../ mod ELS / users.js); 

module.exports = function(app){

app.get('/',function(req,res,next){
model.get_recent函数(错误,结果){
//与你的结果做的东西
res.render('index');
});
});

}

models / users.js

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


Say i have this code to separate routes in 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:

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

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

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?

解决方案

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

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

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

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

Small example for your app above:

app.js

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

routes/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

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

这篇关于Nodejs / Expressjs应用程序结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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