Tuesday 23 May 2017

Dynamic URL doesn't work with HTML5 angularjs

I'm creating an app in AngularJS and ExpressJS and when I try to use dynamic url, with the angular state, it doesn't work properly. It's working when I access directly the URL but only static URLs.

server.js

var express = require('express'),
server = express(),
path = require('path'),
port = process.env.PORT || 8081;

server.use(express.static(__dirname));
server.all("/*", function(req, res, next) {
        res.sendFile(path.join(__dirname, 'index.html'));
    });
server.listen(port);
console.log('Use port ' + port + ' to connect to this server');

exports = module.exports = server;

appRoute.js

angular
    .module('appRoutes', ["ui.router"])
    .config(['$stateProvider', '$urlRouterProvider', '$locationProvider',
    function($stateProvider, $urlRouterProvider, $locationProvider) {

    $stateProvider
        .state({
            name: 'home',
            url: '/home',
            templateUrl: 'public/components/auth/templates/home.html',
        })
        .state({
            name: 'set-password',
            url: '/register/:hashcode',
            templateUrl: 'public/components/register/templates/register.html',
            controller: 'RegisterController'
        });

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

I got a JavaScript error in the browser console, when I try to access an URL like 'http://localhost:8081/register/1234'. The error is:

SyntaxError: expected expression, got '<' angular

I feel that I need to change something in the server configuration but I don't know what exactly.



via JefersonM

No comments:

Post a Comment