express icon indicating copy to clipboard operation
express copied to clipboard

Express and Angular's html5Mode

Open OKNoah opened this issue 11 years ago • 21 comments

I've been trying to use Node and Express with Angular's html5Mode enabled. I keep running into problems.

Here is the first one: http://stackoverflow.com/questions/31327439/angular-routing-in-html5mode-with-node-js

And here is the latest one: http://stackoverflow.com/questions/32044247/node-express-gives-two-responses-breaks-front-end-script

In short, I am currently trying to detect Facebook's user agent and reroute the traffic. I enabled html5Mode so I could route the URL's properly without the hashtag (#) being in the URL. But some errors occur depending on how this is configured, such as the page saying Cannot GET /.../... when I refresh the page, or the page loading twice which breaks Mixpanel and the site.

OKNoah avatar Aug 17 '15 23:08 OKNoah

I think I've narrowed this down to not having anything to do with the route I made. I get the same problems using just combinations of .sendFile() and express.static().

OKNoah avatar Aug 20 '15 17:08 OKNoah

Hi! I was hoping someone who knew about client-side programming and Angular would jump in, but nothing so far :( Were you able to figure out an answer? Anything I can help with were Angular is removed from the equation?

dougwilson avatar Aug 27 '15 19:08 dougwilson

No, I didn't find a solution to this and had to disable html5Mode and forget about Facebook Open Graph working. A workaround was to use Facebook share links with the Open Graph data included. But I would still like html5Mode to work.

OKNoah avatar Aug 28 '15 15:08 OKNoah

Ping @jeffbcross - have you seen this problem before?

ritch avatar Sep 23 '15 18:09 ritch

I have to read too many levels deep to understand what the problem is :). @OKNoah could you post a simple explanation of the problem here in the issue?

If the problem is that you want to serve different content depending on the user agent (serve Angular app for real users, serve pre-rendered shareable content for Facebook or other content sniffers), then you should serve special pre-rendered content to any request that contains the Facebook crawler user agent: https://developers.facebook.com/docs/sharing/webmasters/crawler

jeffbcross avatar Oct 08 '15 16:10 jeffbcross

@OKNoah are you still having this issue? Please comment with some more info and we can re-open. For now I'll close the issue.

ritch avatar Oct 19 '15 13:10 ritch

@ritch Yes, I am still having this issue, only now I'm using React.

@jeffbcross I want my JS app to be able to control the routes, without a # being in the URL. This is sometimes called "HTML5 mode". So going to site.com/search actually goes to index.html, but the javascript sees the search parameter and loads whatever content accordingly.

OKNoah avatar Dec 05 '15 17:12 OKNoah

Hi @OKNoah , to me, that sounds like you want to add the following as probably the last route:

app.get('*', function (req, res) {
  res.sendFile('/path/to/your/index.html');
});

dougwilson avatar Dec 05 '15 17:12 dougwilson

Thanks @dougwilson . However, this solution produces a lot of errors in the browser. Like

SyntaxError: Unexpected token '<'

OKNoah avatar Dec 05 '15 18:12 OKNoah

Yea, I don't know. There is just not enough information here to know what you are doing it even how to help. What if you provided a complete git repo we could clone, along with full instructions on how to reproduce the issue? So far, I haven't seen anything that is an Express bug. Typically if you are looking for general help, especially trying to integrate Express with something or general usage questions, I would recommend asking on one of the Express communities (http://expressjs.com/en/resources/community.html), like Gitter. Typically the people here like myself cannot help with more than true Express bugs and need complete details on how to set everything up on our machine to help.

dougwilson avatar Dec 05 '15 19:12 dougwilson

@OKNoah @dougwilson There is simple solution which comes into mind - binding index.html (or other "main" HTML file) to the list of AngularJS application routes:

app.get('/home', function(res, res) {
  res.sendFile('/path/to/index.html');
});

app.get('/about', function(res, res) {
  res.sendFile('/path/to/index.html');
});

To simplify Express code you can add prefix to Angular.js routes. Assume, you have /app/home or /app/about. Then in your Express you can have one route:

app.get(/\/app.*/, function(req, res) {
  res.sendFile('/path/to/index.html');
});

estliberitas avatar Dec 08 '15 22:12 estliberitas

Ah, yes, that can work as well :) sine the path for the route can also be an array of paths, you could also consolidate all the paths into a single array as well:

app.get(['/', '/about', '/home', ...], function(res, res) {

dougwilson avatar Dec 10 '15 12:12 dougwilson

@dougwilson Wow, did not know this, what a shame of me.

estliberitas avatar Dec 10 '15 13:12 estliberitas

The express middleware express-history-api-fallback solved this issue for me: https://www.npmjs.com/package/express-history-api-fallback

coperator avatar Oct 10 '17 09:10 coperator

I have tried the above solutions, but it is still not working for me. Are there any suggestions?

fabioel avatar Dec 03 '17 23:12 fabioel

Are you using Pug ?

kodelio avatar Dec 04 '17 17:12 kodelio

No I am not using pug. Should I be using it?

fabioel avatar Dec 05 '17 08:12 fabioel

No no, the problem was that I was using it.

app.get('*', function (req, res) {
  res.sendFile('/path/to/your/index.html');
});

This code helped me solve this issue. But what is exactly your problem ?

kodelio avatar Dec 05 '17 12:12 kodelio

The problem is that I cannot use AngularJS with html5Mode(true). I have set the fallback options in express like you proposed and I also tried the middlewares, but somehow I cannot get it to work. Also the .htaccess attempt where you rewrite the routes with it, did not work. So I am kind of clueless, and using the html5Mode(false) for now

fabioel avatar Dec 06 '17 13:12 fabioel

Did you set the <base href="/"> ?

kodelio avatar Dec 06 '17 17:12 kodelio