React-Router 4 not handling direct links or refreshed pages - reactjs

This issue has come up before, but the existing answers still aren't working for me. My app uses React-Router 4, with Node/Express on the back end.
I have read Tyler McGinnis's explanation. I am using Browser Router and what McGinnis calls the "Catch-all" solution. My routes look like this:
app.use(express.static(path.join(__dirname, '../client/public')));
app.post('/auth/signin', signin);
// more Auth and CRUD routes
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, '../client/public/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
});
When I manually enter or refresh routes like http://localhost:8357/admin, this solution works fine. However, when I try that with longer routes like http://localhost:8357/admin/create, the app crashes. As far as I can tell, the browser loads index.html but then looks for bundle in http://localhost:8357/admin/bundle.
Edit: I sorta fixed this problem by hardcoding the css and bundle links in index.html, ie replacing <script type="text/javascript" src='bundle/bundle.js'></script> with <script type="text/javascript" src='http://localhost:8357/bundle/bundle.js'></script>. When I deploy I'll have to change those links. Not an elegant solution, but it works. Is there a better way?

I actually don't believe this has anything to do with React Router.
If you want it use a path that's relative to the root of your directory, you need to make sure you have a forward slash at the beginning.
For example: instead of using bundle/bundle.js, you'll want to make sure you use /bundle/bundle.js.
Hope that works for you!

Related

AngularJS html5mode conflicting with third party library

I have a situation with my Angular 1.8.x routing.
In my angularApp.js file, I have html5mode enabled like:
$locationProvider.html5Mode(true);
My NodeJS app does the following:
module.exports = function(express, app){
var router = express.Router();
router.get('/*', function(req, res){
res.render('index.html');
});
app.use('/', router);
};
I do, however, have an issue with a third party library - Snipcart. What that is supposed to do is include E-commerce features to a frontend app. However, Snipcart's "checkout" button links to a URL with # in it and the Snipcart library doesn't work (doesn't go to the checkout and seems to do a few loops of the current page I am on).
My question is simply this - how can I workaround this? html5mode is a must unfortunately but I need to also be able to support links with a # in it.
Thanks in advance!
I don't know if this can be classed as an answer but I spoke to Snipcart and couldn't get it to work. I guess Angular 1 is just indeed that old
You can also consider urls without the hashtag in your project
http://joeljoseph.net/angularjs-remove-hash-from-url/

UI router + node/express JS page reloading unwantedly

I have a strange issue with UI router and Express interacting together.
I've looked for solutions everywhere but nothing seems to be completely fixing the issue working.
When my app is in my /public folder and when i navigate to / it works fine and the url is getting changed as i click links.But whenever i go directly to /random/path ( ui-sref="post/new"> ), it get intercepted by nodejs and say that this route doesn't exist.
I've fixed that issue using from a topic on that issue :
app.use('/', express.static(__dirname + '/public'));
app.use(function (req, res) {
res.sendFile(__dirname + '/public/');
});
And now everything is working fine whenever i navigate directly to the path i want.
Now there is a new issue where the whole page will reload on every path change instead of just behaving like a client side app should and just load the view + controller.
The strange thing is that it's working as expected for 2 or 3 path.
Can anyone help ?
edit :
angular.module('App').config(function($stateProvider, $locationProvider,$urlRouterProvider) {
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/');
$stateProvider
.state('posts-new', {
url:'/post/new',
templateUrl: 'tmpl1.html'
})
.state('posts', {
url:'/posts',
templateUrl: 'tmpl2.html'
});
});
I've also tried with both base url / and <base href="/index.html">
Alright, so your NodeJS route not existing is because of your route.
It works fine for / because it interprets that as /public/index.html.
If you then try and load /post/new, it looks for /public/post/new/index.html - and doesn't find it.
Fix below should work for you.
app.use(function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
Then, you'll need a slightly separate way of serving your templates - basically as static files! I'd personally suggest you use a simple folder for templates, as shown below. Make sure this is above your catchall route above.
app.use('/templates', express.static('/public/templates'));
Oh, and finally - you'll want your <base> tag set to /.

creating my first nodejs,mongo, express angularjs app

I am setting up my first angularjs webapp using nodejs.
The one part i am not sure how to do is how to include my angularapp.js in my index.html?
currently i have
/public
index.html
/javascript
webapp.js ( my angular app js file)
/routes
myroute.js
app.js (my nodejs app )
my myroute.js has and it loads fine
router.get('/', function(req, res, next) {
res.sendFile(path.resolve('public/index.html'));
});
the problem is that i am note sure how to load my webapp.js
in my index.html i have
<script src = "/public/javascript/webapp.js"></script>
And this does not load. So what is the best way to do this. Should i create
a route for everything in /public/javascript/
how do other people do this or what is the best practice
thanks for any help
update....
i added the following to myroute.js
router.get(/javascript/, function(req, res, next){
res.sendFile(path.resolve('public/javascript/webapp.js'));
});
and the javscript file now loads but this is not the way to do it.
I got it now. I've been searching and looks like for security reasons you need to serve the javascripts and other files you wish to allow access to as static files. On app.js add this :
app.use("/static",express.static(__dirname+"/public/javascript"));
Where "static" is just a virtual path you create for an specific folder. Then change src on script:
http://localhost:port/static/webapp.js
or
/static/webapp.js
It worked for me
For more details you should check this out:
http://expressjs.com/es/starter/static-files.html

angular ui-router, html5 mode always refreshes to /

I am trying to use html5mode in angular, so that I can bookmark a page like http:/myhost/products (where /products is a route defined by$stateProviderRef.state(xxx) ).
To that end I've
added $locationProvider.html5Mode(true) to my app config
added 'base href="/"' (with the <>) to my index.html
added the catch all rewrite to my server.js in node.js
app.get('*', function(req, res) {
res.redirect('/');
});
restarted the node server
so what happens is that the app starts ok, all navigation works, I can go to http://myhost/products and everything works well.
However, if I press refresh at this point, I am redirected back to the index page. Looks to me as if ui-router is either losing the path (/products) or I have missed something in the config / setup
I have been browsing through the questions on StackOverflow until my eyes are bleeding, but all of the solutions to similar problems are things that I've already done (base=, redirect etc)
Anyone else has this problem and solved it ? Would be much appreciated if you could share your findings.
Thanks
You shouldn't redirect in server like that
app.get('*', function(req, res) { res.redirect('/'); });
Instead, send same index.html
app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
Take a look at this generator for more
https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/routes.js

Page reload doesn't work when removing # from angular urls

I'm trying to remove # from my angularjs urls using
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
and <base href="/"> in my index.html and i have changed the hrefs in my partials from #/abc to /abc
it works fine when navigating through links within the site, however except my home page, on every other page, when i try to reload the page or copy and paste the link(and hit enter) it gives me error. i have done alot of search online with no luck. i'm hoping someone here has already gone through this and can help me.
thanks
So if you are using jade you shall do the following:
app.get('/', index);
// while index is defined in routes.js and jade is defined as template engine
If you're using angularjs templating engine with normal html files, do the following:
exports.index = function(req, res) {
res.sendfile(__dirname + "/public/index.html");
};
app.get('/', index);

Resources