ExpressJS 中的自耕农 [英] Yeoman inside ExpressJS
问题描述
我仍然想尝试使用 Yeoman 和 Express 运行一个示例.
我尝试了以下操作,并且正常"工作,但我在合并路线时遇到了困难.(为了可读性而过度简化)
mkdir 测试光盘测试表达mkdir 应用程序光盘应用程序mkdir jscd js自耕农角
然后我将 Gruntfile.js 中的output:dist"改为output:../../public"
现在,两个服务器都可以独立运行(例如 yeoman 服务器和 node app.js).我现在还可以运行 'yeoman build' 将缩小的 JS 输出到 express 应用程序中的/public.
我对路由如何合并有点模糊?我想/拉起 Angular 路由而不是 express 路由等.github 上的 angular-express-seed 示例看起来不错,但我仍然希望 Yeoman 集成到项目中.
任何建议将不胜感激.
我会为 yeoman + expressjs 推荐这种结构:
mkdir 应用程序光盘应用程序自耕农角表达 .
所以你的目录树应该是这样的:
<预><代码>.├──应用│ ├── 404.html│ ├── favicon.ico│ ├── index.html│ ├── robots.txt│ ├── 脚本│ │ ├──控制器│ │ │ └── main.js│ │ ├── 供应商│ │ │ ├── angular.js│ │ │ ├── angular.min.js│ │ │ ├── es5-shim.min.js│ │ │ └── json3.min.js│ │ └── yeoman-test.js│ ├── 样式│ │ └── main.css│ └── 浏览量│ └── main.html├── app.js├── Gruntfile.js├── package.json├── 公共│ ├── 图片│ ├── javascripts│ └── 样式表│ └── style.css├── 路线│ ├── index.js│ └── user.js├──测试│ ├── lib│ │ └── angular-mocks.js│ └── 规格│ └── 控制器│ └── main.js├── testacular.conf.js└── 浏览量├── index.jade└── layout.jade您可以删除现在多余的 public
目录(我们将改为从 app
提供服务):
rm -rf public
现在在 app.js
中,您需要更改从哪个目录提供静态文件.更改这一行:
app.use(express.static(path.join(__dirname, 'public')));
为此:
app.use(express.static(path.join(__dirname, 'app')));
应该就是这样.有一点需要注意的是,您现在有两个索引"文件——一个在 views/index.jade
中,另一个在 app/index.html
中.删除 app/index.html
目前会破坏 yeoman,所以我的建议是去掉 app/index.jade
的路由,只对 index 进行编辑.html
.
希望这会有所帮助!
I'd still like to try to get an example running w/ Yeoman and Express.
I tried the following and it worked "okay", but I'm stuck merging the routes. (over simplified for readability)
mkdir test
cd test
express
mkdir app
cd app
mkdir js
cd js
yeoman angular
Then I changed "output:dist" to "output:../../public" in the Gruntfile.js
Now, both servers run okay on their own (e.g. yeoman server and node app.js). I can also run 'yeoman build' now to output the minified JS to /public in the express app.
I'm a bit fuzzy on how the routes might merge? I would like / to pull up the Angular route and not the express route, etc. The angular-express-seed examples on github look okay, but I would still like Yeoman integrated into the project.
Any suggestions would be appreciated.
I would recommend this structure for yeoman + expressjs:
mkdir app
cd app
yeoman angular
express .
So your dir tree should look like this:
.
├── app
│ ├── 404.html
│ ├── favicon.ico
│ ├── index.html
│ ├── robots.txt
│ ├── scripts
│ │ ├── controllers
│ │ │ └── main.js
│ │ ├── vendor
│ │ │ ├── angular.js
│ │ │ ├── angular.min.js
│ │ │ ├── es5-shim.min.js
│ │ │ └── json3.min.js
│ │ └── yeoman-test.js
│ ├── styles
│ │ └── main.css
│ └── views
│ └── main.html
├── app.js
├── Gruntfile.js
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── user.js
├── test
│ ├── lib
│ │ └── angular-mocks.js
│ └── spec
│ └── controllers
│ └── main.js
├── testacular.conf.js
└── views
├── index.jade
└── layout.jade
You can remove the now redundant public
directory (we're going to serve from app
instead):
rm -rf public
And now in app.js
, you need to change which dir to serve static files from. Change this line:
app.use(express.static(path.join(__dirname, 'public')));
to this:
app.use(express.static(path.join(__dirname, 'app')));
And that should be about it. There's one careat in that you now have two "index" files -- one in views/index.jade
, and the other in app/index.html
. Removing the app/index.html
currently breaks yeoman, so my advice is to get rid of the route for app/index.jade
and just make edits to index.html
.
Hope this helps!
这篇关于ExpressJS 中的自耕农的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!