I've got in impasse with setting Angular routes to work with Express.
I tried to do like here Express 4, NodeJS, AngularJS routing but that did not work. The static index.html serves each time the url changes, but the Angular does not catch the partial.
In my Express I have the following code:
var express = require("express");
var app = express();
app.use(express.static(__dirname + '/app'));
app.use("*",function(req,res){
res.sendFile(path.join(__dirname,"app/index.html"));
});
app.listen(1337, function(){
console.log("Server is listening on port 1337");
});
And in my Angular app, I have the following:
var app = angular.module("myapp",["ngRoute","appControllers"]);
app.config(["$routeProvider","$locationProvider",function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$routeProvider
.when("/list",{
templateUrl: "/partials/users.html",
controller: "userListCntr"
})
.when("/edit", {
templateUrl: "/partials/edit.html",
controller: "userEditCntr"
})
.when("/register", {
templateUrl: "/partials/register.html",
controller: "registerCntr"
})
.when("/login", {
templateUrl: "/partials/login.html",
controller: "registerCntr"
});
}]);
I don't have any errors in my browser console. The html inspector shows:
<!-- ngView: -->
so the directive is kind of initialized.
And dependency list:
dependencies": {
"angular": "1.3.x",
"angular-mocks": "1.3.x",
"jquery": "1.10.2",
"bootstrap": "~3.1.1",
"angular-route": "1.3.x",
"angular-resource": "1.3.x",
"angular-animate": "1.3.x"
}
here is my file structure:
--app (angular app)
--components
--js
--css
--img
--assets
--partials
--index.html
--node-modules
--server.js (here express settigs are)
ALso, I made changes as follows to account for all static resources, but still does not work:
app.use(express.static(__dirname+ "/app"));
app.use('/js', express.static(__dirname + '/app/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/app/css'));
app.use('/assets', express.static(__dirname + '/app/assets'));
app.use('/components', express.static(__dirname + '/app/components'));
app.use('/img', express.static(__dirname + '/app/img'));
app.use('/partials', express.static(__dirname + '/app/partials'));
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('/app/index.html', { root: __dirname });
});
Please, help, because I can't find a solution. Thanks!
When you don't pass a path to express.use()
, then it defaults to /
. The proceeding rule you set for *
is either redundant or may not even work.
Also, since you're using html5mode
, you need to explicitly set apart routes from resources so Express doesn't try to serve up your index.html
file for all requests.
Try this example from Angular UI-Router on for size:
var express = require('express');
var app = express();
app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
app.listen(3006); //the port you want to use
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With