Jan sebastian Jan sebastian - 2 months ago 28
Node.js Question

angularjs and expressjs route crash (Aw, Snap!)

I create routing in angularjs and expressjs and create

app.all('/*'...)
to make it can rander index.html, but every time I use
/*
the page will gona crash (Aw, Snap!)

angularjs

home.config(function($routeProvider,$locationProvider,$httpProvider)
{

$routeProvider.when('/home',
{
templateUrl: 'home.html',

})
.when('/access_level',
{
templateUrl: 'access_level.html',

})
.when('/Users',
{
templateUrl: 'Users.html',

})
.otherwise({
redirectTo: '/home'
});
$locationProvider.html5Mode({enabled: true,requireBase: false,rewriteLinks: true});
});


expressjs

app.all('/*', function(req, res, next) {
res.sendFile('/views/index.html', { root: __dirname });
});
app.get('/:name',function(req,res){
var name = req.params.name;
res.render(name);
});


so what's the matter with my code?

Answer

finaly I found another solution, even I didn't use html5 mode in angularjs, but it's work

so, I only make views folder work's like public folder

change from

app.set('views', path.join(__dirname, 'views'));

to

app.use(express.static(path.join(__dirname, 'views')));

and then I only create / server side route (I didn't need /:name anymore)

app.get('/', function(req, res) {
    res.sendFile('/views/index.html', { root: __dirname });
});