0

I have a login page an then I have multiple other pages that follows the login page. I am using Express and chose ejs as my template engine and I created 2 ejs files: 1 for my login page and 1 for the rest of my pages.

My app.js app.get looks like follows:

app.get('/', function (req, res) {
    res.render("login", { client_id: client_id, app_url: app_url});
});
app.get('*', function (req, res) {
    res.render("index", { client_id: client_id, app_url: app_url});
});

My controller loads the login page and then loads the dashboard

function config($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");
     $stateProvider
        .state('login', {
            url: "/",
            templateUrl: "views/login.html",

    })

    $stateProvider
        .state('dashboard_1', {
            url: "/dashboard",
            templateUrl: "views/dashboard.html",
    })

When i navigate to / the login.ejs must be loaded when I navigate to any other url my indes.ejs must be loaded. How do I get this to work?

Thys Andries Michels
  • 761
  • 4
  • 11
  • 23

1 Answers1

1

* will work for all URLs including /, so you can do something like this,

app.get('*', function (req, res) {
    if ( req.path == '/'){
       res.render("login", { client_id: client_id, app_url: app_url});
    }
    else{
       res.render("index", { client_id: client_id, app_url: app_url});
    }
});
Ravi
  • 1,320
  • 12
  • 19
  • Thanks for the response, but seems like req.path is always == '/' i logged req.path and confirmed. Why is it not resolving the path? – Thys Andries Michels Oct 07 '14 at 14:40
  • well, if you hit URL `http://host:port/`, then the `req.url` will be `/`, and if you go to `http://host:port/index`, then the `req.url` will be `/index` – Ravi Oct 07 '14 at 15:14
  • As I am using angular my url looks like `http://host:port/#/` for login and the rest will be `http://host:port/#/*` – Thys Andries Michels Oct 07 '14 at 15:32
  • [The part of the URL after the '#' mark, called the fragment, is not sent to the server. If you store data in the fragment, then it is up to you to process that data and do an ajax request with the data in a GET argument.](http://stackoverflow.com/questions/9967681/nodejs-url-with-hash?answertab=active#tab-top) – Ravi Oct 08 '14 at 04:29