Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Struck with routing issue in expressjs and AngularJs project.

It's not a single page application and I am not using any view engines such as jade.

We are just using plain HTML.

I am working on password reset functionality where user can reset the password by clicking a link provided by an email. So I assume there won't be any route change event in the context of Angular (Please correct me if I am wrong).

And my express configurations are as follows.

routes = require('./routes/index');    
app.configure(function () {
    app.use(express.static(__dirname + '/app'));
    app.use('/css', express.static(__dirname + '/app/css'));
    app.set('views', __dirname + '/app');
    app.set("view options", { layout: false });
    app.engine('.html', require('ejs').__express);
    app.set('view engine', 'html');
    app.use(express.favicon());
    //app.use(require('connect').bodyParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());

    app.use(app.router);

});


// Routes

app.get('/', routes.index);

app.get('/resetpassword.html/:resetcode', function (req, res) {
    console.log("reset code: " + req.params.resetcode);
    res.render('resetpassword.html/' + req.params.resetcode);
});

app.get('/api', function (req, res) {
    res.send('Ecomm API is running');
});

// JSON API
app.post('/registeruser', usersApi.registerUser);
app.post('/api/login', usersApi.logIn);
app.post('/api/addgame', gamesApi.addGame);

app.get('*', routes.index);

// Start server

app.listen(2221, function () {
    console.log("Express server listening on port %d in %s mode", 2221, app.settings.env);
});

and index.js

exports.index = function(req, res){
  res.render('home.html');
}; // Always rending index.html which needs to be fixed.

And app.js from AnghularJs as follows

app.config(function ($routeProvider) {
    $routeProvider
      .when('/', { templateUrl: 'home.html' })
      .when('/resetpassword.html/:resetcode', { templateUrl: '/resetpassword.html', controller: 'ResetPasswordCtrl' })
      .otherwise({ redirectTo: '/' });
});

I am getting 500 internal error or view not found error.

Any suggestions please.

share|improve this question
add comment

1 Answer

You are concatenating the password to the view name that you pass to render hence why Express does not find the view and returns a 500 error. You need to pass the data as an additional parameter to the render function as an object:

res.render('resetpassword.html', {resetcode: req.params.resetcode} );

Then in your view use resetcode directly e.g.

<span><%= resetcode %></span>
share|improve this answer
 
thanks for your reply. Instead of accessing the param from view, can't we read it from controller? –  Naresh yesterday
 
Maybe but that's independent from the template rendering. If the url is /resetpassword.html/1234 then I guess Angular controller will be able to read it, but you don't need to pass it to the server side node.js render function. IMO you shouldn't be doing routing on both server and client side, as you do routing on server side, the routing on Angular side seems redundant. Btw, pls mark the answer as accepted if that solves your issue ;) –  jtblin yesterday
 
thanks again. But after the above changes I am not getting any error. But I can't see the url. I can just see the host. any idea? –  Naresh yesterday
 
I think I have resolved the other problem which was modifying the url. I had a client side routing which was setting the route to '/'. Still trying to figure out how to get the params on Angular side. –  Naresh yesterday
 
Cool that's what I thought too. As I said you probably should not be using routes on Angular side. Just have the respective controller code for each teamplate separately. Also pls mark the answer as accepted as it solved your issue ;) –  jtblin yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.