我正在使用NodeJS的ExpressJS web框架。

使用ExpressJS的人把他们的环境(开发、生产、测试……),他们的路线等放在app.js上。我认为这不是一个美好的方式,因为当你有一个大的应用程序,app.js太大了!

我想要这样的目录结构:

| my-application
| -- app.js
| -- config/
     | -- environment.js
     | -- routes.js

这是我的代码:

app.js

var express = require('express');
var app = module.exports = express.createServer();

require('./config/environment.js')(app, express);
require('./config/routes.js')(app);

app.listen(3000);

配置/ environment.js

module.exports = function(app, express){
    app.configure(function() {
    app.use(express.logger());
    });

    app.configure('development', function() {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
    });

    app.configure('production', function() {
    app.use(express.errorHandler());
    });
};

配置/ routes.js

module.exports = function(app) {
    app.get('/', function(req, res) {
    res.send('Hello world !');
    });
};

我的代码工作得很好,我认为目录的结构很漂亮。然而,代码必须进行调整,我不确定它是否好/漂亮。

是更好地使用我的目录结构和调整代码或简单地使用一个文件(app.js)?

谢谢你的建议!


当前回答

现在是2015年底,在开发我的结构3年之后,在小型和大型项目中。结论?

不做一个大的MVC,而是在模块中分离

所以…

Why?

通常一个人在一个模块上工作(例如产品),你可以独立地更改它。 您可以重用模块 你可以单独测试它 你可以单独替换它 它们有清晰(稳定)的接口 -至少,如果有多个开发人员工作,模块分离是有帮助的

nodebootstrap项目的方法与我的最终结构类似。(github)

这个结构是什么样的?

小型封装模块,每个模块都有独立的MVC 每个模块都有一个package.json 测试作为结构的一部分(在每个模块中) 全局配置、库和服务 集成Docker,集群,永远

文件夹概述(参见lib文件夹中的模块):

其他回答

我的问题是在2011年4月提出的,已经很老了。在这段时间里,我可以提高我使用Express.js的经验,以及如何构建使用这个库编写的应用程序。所以,我在这里分享我的经验。

这是我的目录结构:

├── app.js   // main entry
├── config   // The configuration of my applications (logger, global config, ...)
├── models   // The model data (e.g. Mongoose model)
├── public   // The public directory (client-side code)
├── routes   // The route definitions and implementations
├── services // The standalone services (Database service, Email service, ...)
└── views    // The view rendered by the server to the client (e.g. Jade, EJS, ...)

App.js

app.js文件的目标是引导expressjs应用程序。它加载配置模块,记录器模块,等待数据库连接,…,并运行快速服务器。

'use strict';
require('./config');
var database = require('./services/database');
var express = require('express');
var app = express();
module.exports = app;

function main() {
  var http = require('http');

  // Configure the application.
  app.configure(function () {
    // ... ... ...
  });
  app.configure('production', function () {
    // ... ... ...
  });
  app.configure('development', function () {
    // ... ... ...
  });

  var server = http.createServer(app);

  // Load all routes.
  require('./routes')(app);

  // Listen on http port.
  server.listen(3000);
}

database.connect(function (err) {
  if (err) { 
    // ...
  }
  main();
});

路线/

routes目录有一个index.js文件。它的目标是引入一种魔法来加载routes/目录中的所有其他文件。实现如下:

/**
 * This module loads dynamically all routes modules located in the routes/
 * directory.
 */
'use strict';
var fs = require('fs');
var path = require('path');

module.exports = function (app) {
  fs.readdirSync('./routes').forEach(function (file) {
    // Avoid to read this current file.
    if (file === path.basename(__filename)) { return; }

    // Load the route file.
    require('./' + file)(app);
  });
};

使用该模块,创建一个新的路由定义和实现非常容易。例如,hello.js:

function hello(req, res) {
  res.send('Hello world');
}

module.exports = function (app) {
  app.get('/api/hello_world', hello);
};

每个路由模块都是独立的。

我喜欢使用全局“应用程序”,而不是导出一个函数等

你可能会感兴趣:

https://github.com/flatiron/nconf

带有文件、环境变量、命令行参数和原子对象合并的分层node.js配置。

使用handlebar和Passportjs的ExpressJs项目MVC结构的最佳方法

- app
      -config 
        -passport-setup.js
      -controllers
      -middleware
      -models
      -routes
      -service
    -bin
      -www
      -configuration.js
      -passport.js
    -node_modules
    -views
     -handlebars page
    -env
    -.gitignore
    -package.json
    -package-lock.json

我给MVC风格的文件夹结构,请找到下面。

我们在大型和中型web应用程序中使用了波纹文件夹结构。

 myapp   
|
|
|____app
|      |____controllers
|      |    |____home.js
|      |
|      |____models
|      |     |___home.js
|      |
|      |____views
|           |___404.ejs
|           |___error.ejs
|           |___index.ejs
|           |___login.ejs
|           |___signup.ejs
|   
|
|_____config
|     |___auth.js
|     |___constants.js
|     |___database.js
|     |___passport.js
|     |___routes.js
|
|
|____lib
|    |___email.js
|
|____node_modules
|
|
|____public.js
|    |____css
|    |    |__style.css
|    |    
|    |____js
|    |    |__script.js
|    |
|    |____img
|    |    |__img.jpg
|    |
|    |
|    |____uploads
|         |__img.jpg
|      
|   
|
|_____app.js
|
|
|
|_____package.json

我已经为生成express mvc文件夹结构器创建了一个npm模块。

请找到以下https://www.npmjs.com/package/express-mvc-generator

只需简单的步骤来生成和使用这个模块。

I)安装模块NPM

Ii)检查选项express -h

iii)生成express mvc结构express myapp

iv)安装依赖项:

v)打开config/database.js, Please configure your mongo db。

vi)运行应用节点app或nodemon app

vii)查看网址http://localhost:8042/signup或http://yourip:8042/signup