node.js - Matching all urls using router.all() method of Express and using Angular ui-router in the same time -


problem solved. see answer details.

i've written authentication part of app long ago (there few jquery ajax calls), moved away jquery angular, won't using express.js routing anymore.

i used match "/" (localhost) that.

router.get('/', csrfprotection, indexonly, indexcontroller.index); 

i'll using ui-router, serve 1 index.html file , angular handle rest. don't want rewrite in index page, want make exception that.

if user visits homepage, check whether they're logged in or not. if aren't, regular login page gets rendered, (frontend/index). if logged in , try visit main page (localhost), redirected /feed. indexonly checks , csrfprotection, know, protects csrf. need 2 of these work on index if user not logged in.

here's indexcontroller.js

exports.index = function(req, res) {   res.render('frontend/index', {     csrftoken: req.csrftoken()   }); }; 

how can that? tried little couldn't it. turned unlimited loop, guess.

index.js (router)

var express = require('express'); var router = express.router(); var indexcontroller = require('../../controllers/frontend/indexcontroller'); var indexonly = require('../../app').indexonly; var csrf = require('csurf'); var csrfprotection = csrf({   cookie: true });  router.all('/*', function(req,res,next) {   if(req.originalurl == '/') {     router.get('/', csrfprotection, indexonly, indexcontroller.index);   } else {     res.sendfile('views/index.html', {       root : __dirname     });   } }); 

if use below, index page works 403 forbidden angular part.

router.get('/', csrfprotection, indexonly, indexcontroller.index);  router.all('/*', function(req,res,next) {   if(req.originalurl == '/') {     next();   } else {     res.sendfile('../../views/index5.html', {       root : __dirname     });   } }); 

okay, solved problem. solution below.

the problem wanted both keep old jquery/express.js stack serve index.jade file unlogged/unregistered users , serve routes using angular ui-router.

here's full index.js (router) file

var express = require('express'); var router = express.router(); var indexcontroller = require('../../controllers/frontend/indexcontroller'); var indexonly = require('../../app').indexonly;  var csrf = require('csurf'); var csrfprotection = csrf({   cookie: true });  var path = require('path');  router.get('/', csrfprotection, indexonly, indexcontroller.index);  router.all('/*', function(req,res,next) {   if(req.originalurl == '/') {     next(); // doesn't anything, allows route above work   } else {     res.sendfile(path.resolve('views/index.html'));   } }); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -