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

查看:113
本文介绍了如何构建一个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

(假设我们正在从路径文件夹工作,需要去1级,然后到模型)

表达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 也看起来非常有趣。它类似于流星,没有所有的炒作,实际上给予信用(特别是节点和快递)。

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的粉丝(我不是)和reeeeaaaaaally想要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,不介意流血某些概念有 机车 。它是一个基于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).

即使您不打算使用它。

一个href =https://github.com/madhums/nodejs-express-mongoose-demo> 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天全站免登陆