0

So my question is: how could I make angular and express routing work together with html5mode enabled? When I change state from '/' to my admins state I get list of all admins and everything is fine until I refresh my page. Then I get only json result of admins but my view disappears. I copied only peace of code which is relevant - to make it shorter, there is no syntax errors.

Any help is appreciated. Thanks.

My file structure:

+-- client
|   app
|       admin.config.js
|       admin.js
|       admin.module.js
|   assets
|   views
|       admin
|           admin-edit.ejs
|           admin-list.ejs
|   lib
|       bower components
+-- server
|   routes
|       index.js
|       admins.js
|   models
|       Admin.js
|   config
|       dbconfig.js
|   server.js

In my Angular module.config I have:

function config($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider
        .state('admins', {
            url        : '/admins',
            templateUrl: '/views/admin/admin-list.ejs',
            controller : 'AdminController',
            resolve    : {
                loginRequired: loginRequired,
                getAdminList : getAdminList
            }
        });

    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/admins');

My server.js file looks like:

/* Routes */
var routes = require('./routes/index');
var auth   = require('./routes/auth');
var users  = require('./routes/users');
var admins = require('./routes/admins');


var app = express();


/*************************************
 App initialization
 **************************************/

/* Auto increment mongoose plugin */
autoIncrement.initialize(connection);

/* Auth secret code*/
app.set('superSecret', dbconfig.SECRET);


/* View engine setup */
app.set('views', path.join(__dirname, '../client/views'));
app.set('view engine', 'ejs');


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'client', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static('client'));

app.use('/', routes);
app.use('/auth', auth);
app.use('/users', users);
app.use('/admins', admins);

My index route looks like:

var express = require('express');
var router  = express.Router();

/* GET home page. */
router.get('/', function (req, res) {
    res.render('index');
});


module.exports = router;

I tried adding this into my server.js

app.get('*', function (req, res){
        res.sendFile(path.join(__dirname + '/client/index.ejs'));
});

and in this case when I refresh page while my url is /admins I get downloaded index page.

I also tried adding in my index.js route

router.get('*', function (req, res) {
   res.render('index');
})

and as result of adding this is endless loop in my admins list.

In my index.ejs I have

<meta charset="utf-8">
<base href="/">

2 Answers 2

0

That's a lot of code. I want to give you this. It is an answer I posted yesterday that I am proud of. It is a simple example of Angular routing in Angular 1.5. You will have to add your line about $locationProvider.html5Mode(true); because I am not using it.

Sign up to request clarification or add additional context in comments.

Comments

0

After some time I found solution here, instead of specific pages I added asterisk (*) to every get request I made so now my auth route looks like:

router.route('/signup')

    /* Render index by default */
    .get(function (req, res) {
        res.render('index', {
            page: req.params.page
        });

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.