如何构建 express.js 应用程序? [英] How to structure a express.js application?

查看:37
本文介绍了如何构建 express.js 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Express.js 应用程序中,是否有分解和模块化 app.js 文件的通用约定?还是将所有内容保存在一个文件中很常见?

Is there a common convention for breaking up and modularizing the app.js file in an Express.js application? Or is it common to keep everything in a single file?

推荐答案

我的解决方案如下:

~/app
|~controllers
| |-monkey.js
| |-zoo.js
|~models
| |-monkey.js
| |-zoo.js
|~views
| |~zoos
|   |-new.jade
|   |-_form.jade
|~test
|  |~controllers
|    |-zoo.js
|  |~models
|    |-zoo.js
|-index.js

我使用导出来返回相关内容.例如,在我做的模型中:

I use Exports to return what's relevant. For instance, in the models I do:

module.exports = mongoose.model('PhoneNumber', PhoneNumberSchema);

然后如果我需要创建一个电话号码,就这么简单:

and then if I need to create a phone number, it's as simple as:

var PhoneNumber = require('../models/phoneNumber');
var phoneNumber = new PhoneNumber();

如果我需要使用架构,那么 PhoneNumber.schema

if I need to use the schema, then PhoneNumber.schema

(假设我们从 routes 文件夹工作,需要向上一级然后向下一级到模型)

express wiki 有一个构建在上面的框架列表

The express wiki has a list of frameworks built on top of it.

其中,我认为 Twitter 的斗牛士 结构很好.我们实际上使用了一种非常相似的方法来加载应用程序的各个部分.

Of those, I think Twitter's matador is structured pretty well. We actually used a very similar approach to how they load up parts of the app.

derby.js 看起来也非常有趣.它类似于 meteor 没有所有的炒作,实际上在信用到期时给予信用(特别是节点和快递).

derby.js also looks extremely interesting. It's akin to meteor without all of the hype and actually gives credit where credit is due (notably, node and express).

如果您是 CoffeeScript 的粉丝(我不是)并且 reeeeaaaaaaly 想要 Rails 的 L&F,那么还有 Tower.js.

If you are a fan of CoffeeScript (I am not) and reeeeaaaaaally want the L&F of Rails, there is also Tower.js.

如果您熟悉 Rails 并且不介意某些概念的混淆,则可以使用 Locomotive.它是一个基于 Express 构建的轻量级框架.它的结构与 RoR 非常相似,并继承了一些更基本的概念(例如路由).

If you are familiar with Rails and don't mind the bleed-over of some concepts there is Locomotive. It is a light-weight framework built on Express. It has a very similar structure as RoR and carries over some of the more rudimentary concepts (such as routing).

即使您不打算使用它,也值得一试.

It's worth checking out even if you don't plan to use it.

nodejs-express-mongoose-demo与我的结构非常相似.看看吧.

nodejs-express-mongoose-demo is very similar to how I have mine structured. Check it out.

这篇关于如何构建 express.js 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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