Skip to content Skip to sidebar Skip to footer

How To Use Multiple Index Pages In Angularjs

I want to access the localhost:3000/admin which is in my views .. the index.html and the admin.html are my two different base files one is for users and the other is for admin dash

Solution 1:

As I understand your question, you would want to just add one more routing rule to express. Express uses the first route that matches, so order is important here.

app.get('/admin/*', function(req, res){
    res.sendFile(__dirname + '/public/app/views/admin.html');
});

app.get('*', function(req, res){
    res.sendFile(__dirname + '/public/app/views/index.html');
});

The angular docs state about html5mode that you should rewrite all your urls to a single app entry point: Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).https://docs.angularjs.org/guide/$location#server-side

So to be clear: What I suggest is that you create server-routes for two separate apps. Nothing prevents you from using the first app one more time on another route, but I would advice against it. Separate anything with real power over your backend from the public app. I.e. remove /admin/login and /admin from your ng-routes and simply create a separate app for that.

Solution 2:

I found a better solution for this..

app.get('*', function(req, res){
if (req.url.match('^\/admin')) {
    res.sendFile(__dirname + '/public/app/views/admin.html');
} else {
    res.sendFile(__dirname + '/public/app/views/index.html');
}
});

Post a Comment for "How To Use Multiple Index Pages In Angularjs"