Reputation: 365
In my app.js I have
$routeProvider
.when('/', {
templateUrl: '/views/main.html'
})
.when('/admin', {
templateUrl: 'views/admin.html',
controller: 'admin',
})
so my routes are localhost:3000/#/ and on click of button it takes me to localhost:3000/#/admin I wanted to remove # from my url so I used
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
now localhost:3000/
is working and on click of button it takes me to localhost:3000/admin
.
But if I directly give localhost:3000/admin
in url,it is giving Cannot Get /admin..But if give localhost:3000/#/admin
,it is taking me to localhost:3000/admin
.Why this behaviour how can I make localhost:3000/admin
to work.Can someone help
Upvotes: 1
Views: 68
Reputation: 4700
Move this line
app.use(express.static(path.join(__dirname, 'public')));
before app.use(app.router);
in app.js file
add this line in javascripts/app.js
$locationProvider.html5Mode({ enabled: true, requireBase: false });
add this code
app.get('*', function(req, res) {
res.render('index', {
title: 'Express'
});
});
in app.js
before http.createServer(app)
line
Upvotes: 1
Reputation: 11
Please update your html as below:
in the head section , please use 'base' tag. So head tag will look like something :
<html>
<head>
<base href="/">
</head>
</html>
then try using the URL 'localhost:3000/admin' Let me know if this helps.
Upvotes: 0
Reputation: 111336
You would need to make all of those routes return the same HTML and other assets, so that the first request to a given URL returns the correct HTML.
With the '#' it's much easier because everything from '#' to the end of the URL is not sent in the request to the server, so the server doesn't have to do anything.
But for urls without the '#' the entire URL is sent to the server so the server has to know what to serve, and the client has to be able to load a correct view depending on which URL was used in the first page load.
With no backend code provided it's hard to tell anything specific, but you have to make sure that:
http://localhost:3000/admin
the same HTML is returned as for http://localhost:3000/
Upvotes: 0