Yeoman inside ExpressJS

kyleroche picture kyleroche · Dec 31, 2012 · Viewed 8.2k times · Source

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.

Answer

btford picture btford · Jan 1, 2013

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!