Caktus
Caktus

Reputation: 41

Error 500 while using routes in Express

I am trying to build an application using nodejs and express. I am currently facing a weird problem.

Here is how it looks so far :

var express = require('express');
var routes = require('./routes'); //here I have all functions for routing
app.use(app.router);
app.get("/", routes.index)
   .get("/login", routes.login)
   .get("/restricted/menu", routes.menu)
   .get("/restricted/myapp", routes.datePicker) //I send to a page where the user select a date
   .get("/restricted/myapp/:datetoget", routes.myapp) //after selecting the date, the user is redirected to a URL like this : /restricted/myapp/2014-01-30
   .use(route.allOther);
   .... 

The problems appear when I try to reach the page :

host/restricted/myapp/

without filling the "datetoget" parameter. This is the console when I try to reach this page

HTTP Express server listening on port 3000
undefined
GET /restricted/myapp/ 500 8ms

As soon as I put something after myapp/ it works (ex: /host/restricted/myapp/something). Here is a part of the code for the routes.myapp function:

function(req,res){
    console.log('into myapp'); //this step seems not to be reached
    var date;
    if(req.params.datetoget){
        date = new Date(req.params.datetoget);
        if(!utilities.isDate(date)){
             date = new Date();
        }
    }else{
        date = new Date();
    }
    ...
}

Is it also seems that the last

.use(routes.allOther)

does not work because I gent the standard 500 express error page. I also tried to put a new route like this

.get('/restricted/myapp/', routes.myapp)

It did not solve the problem.

Just in case this is the routes.datePicker

var notInDir = true;
for(var dirKey in directories){ //directories is an object where I store string of directories
    var dir = directories[dirKey];
    if(req.originalUrl === dir.ref){ //The origin must be known
        var datePicker = {
                title:"Select the date",
                jsFile: [array of Javascript needed files],
                cssFile: [array of css needed files],
                mode: "datebox",
                ref: req.originalUrl
            };
        res.render("/restricted/datepicker.ejs",datePicker);
        notInDir = false;
    }
}
if(notInDir){
    throw "Error";
}

I am a bit lost so far and a little help would be appreciate :-) Thanks a lot.

Upvotes: 2

Views: 6020

Answers (1)

Caktus
Caktus

Reputation: 41

It seems that express considers this route :

/restricted/myapp

and this route :

/restricted/myapp/

As the same. So the function called is routes.datePicker. But In my case, the origin (/restricted/myapp/) was not defined in ''directory'', only was /restricted/myapp, that's why the exception is throw.

Upvotes: 1

Related Questions