generator icon indicating copy to clipboard operation
generator copied to clipboard

yeoman server & Angular

Open addyosmani opened this issue 14 years ago • 7 comments

from @Hoverbear

Hi, If you create an angular project with yeoman init angular, then add a second view to the project (For example views/alt.html) and follow a link to that page it works fine, cool.

However, if I edit the views/alt.html page while running yeoman server yeoman will reload the page and yield a 404 like so: http://i.imgur.com/UBSwk.png

A working example can be found here: Here (In theory you can just clone and nav to dir then run yeoman server)

To reproduce:

  • Run yeoman server
  • Navigate to the /alt link (in red)
  • Edit the /views/alt.html file and save.
  • Observe a 404.

Previous conversation about this issue is in https://github.com/yeoman/yeoman/issues/584

addyosmani avatar Oct 10 '12 10:10 addyosmani

@Hoverbear Right now, if you're using yeoman server to launch this, the only way to have all your routes work and reloadable is to use the hash style href="/#/alt".

The angular generator leaves out $locationProvider.html5Mode(true); in the app.js file after configuring the routes. We can put this back in, but we would run into the issue you describe where you can navigate to the /alt link, but you can't reload it.

The problem is that the yeoman server doesn't have a default route which serves the index.html file. If that were the case, you could use the route /alt and serve the index file which would then load views/alt.html.

Theory is that the express server gets a request, if it's not a static file or api route, it should serve the index.html file. That way, you can load up any page in the app from a clean URL. The last line in my express servers is app.get("*", routes.index); It's a catch-all route when all static files and api routes have not matched.

jacobmumm avatar Oct 15 '12 13:10 jacobmumm

@jacobmumm Thanks for the info - I can confirm the same behaviour. I.e. routes load the Yeoman 404 page instead of the correct AngularJS route when using$locationProvider.html5Mode(true);.

@btford I guess a solution would be to get the aJS generator to just create a 404 route in app.js, and point all unknown URL's towards index.html?

dhruv-bhatia avatar Oct 24 '12 10:10 dhruv-bhatia

+1

shairez avatar Jan 08 '13 22:01 shairez

Screen Shot 2013-01-14 at 10 20 47 AM What is the status on this [image attached]? Any workaround for this livereload issue?

  1. yeoman server should either allow configuration options to be passed in, or at least allow easy configuration of the server.
  2. yeoman's server configuration (when using angular and html5Mode=true) should provide a catch all to serve up index.html all the time to allow the angular app to handle the routing inside the app.

In meantime, would love if someone could point towards a temporary workaround to get this to work. Modify yeomans server configuration in the internals somewhere?

NathanWalker avatar Jan 14 '13 18:01 NathanWalker

Here is the temporary WORKAROUND:

Yeoman LiveReload configuration tweak

This is a temporary hack until yeoman's server configuration supports an option to do something like this

pico /usr/local/lib/node_modules/yeoman/tasks/server.js

Ctrl +W, search for: function errorHandler

Paste the below block in between the following:

return function errorHandler(req, res, next) {

fs.readFile('app/index.html',function (err, data){
    res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
    res.write(data);
    res.end();
});
return;

// Figure out the requested path

Ctrl +O (that's the letter 'O') - to SAVE!

Now yeoman should play nice with LiveReload and change watching. No More 404 when LiveReload fires!!

NathanWalker avatar Jan 14 '13 19:01 NathanWalker

@NathanWalker your workaround doesn't work with beta3 (as yeoman is using grunt for server). Any idea how to do it with grunt?

patcito avatar Mar 29 '13 11:03 patcito

@patcito yeoman/generator-angular#132

L42y avatar Mar 29 '13 11:03 L42y