Angularjs node express js后端的路由
Angularjs route for node express js backend
我已经创建了一个带有 angular js 前端的 nodejs 后端。我有以下 angularjs 路由代码。
var myApp = angular.module('myApp ', ['ngRoute']);
myApp .config(['$routeProvider', '$locationProvider', function($routeProvider){
$routeProvider
.when('/login', {
templateUrl : 'login.html',
controller: 'loginController'
}).otherwise({
templateUrl : 'main.html',
controller : 'mainController'
});
}]);
而我的 app.js 包含以下代码。 (注:我使用的是express js框架3.0)
app.use(app.router);
app.use('/',function(req, res) {
res.sendfile(__dirname + '/public/index.html');
});
我把所有的 html 放在 public 文件夹中,结构是这样的。
public/
-css folder
-angular dependencies
-all my htmls (index.html,login.html,main.html)
routes/
-index.js
views/
-index.ejs
-error.ejs
app.js
现在,当我击中我的基地 url 时,index.html 会根据我在上面给出的 app.js 中提供的代码进行加载。然后 angular 在 ng-view div 中插入 main.html 代码。当我点击 /login 时,我希望加载 login.html。但是加载的是相同的 main.html。有人可以阐明这一点吗?解决方案很少,但 none 帮了我的忙。
我想你想要类似 express.static
的东西
app.use(express.static(__dirname + '/public'));
如果收到 "login.html" 的请求,它将首先检查 public 目录中的该文件。
How to use AngularJS routes with Express (Node.js) when a new page is requested?
这对我有帮助。
我做了更改
myApp.config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider){
$routeProvider
.when('/login', {
templateUrl : 'login.html',
controller: 'loginController'
}).when('/', {
templateUrl : 'main.html',
controller : 'mainController'
});
$locationProvider.html5Mode(true);
}]);
更改为 app.js
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.use('/',function(req, res) {
res.sendfile(__dirname + '/public/index.html');
});
我已经创建了一个带有 angular js 前端的 nodejs 后端。我有以下 angularjs 路由代码。
var myApp = angular.module('myApp ', ['ngRoute']);
myApp .config(['$routeProvider', '$locationProvider', function($routeProvider){
$routeProvider
.when('/login', {
templateUrl : 'login.html',
controller: 'loginController'
}).otherwise({
templateUrl : 'main.html',
controller : 'mainController'
});
}]);
而我的 app.js 包含以下代码。 (注:我使用的是express js框架3.0)
app.use(app.router);
app.use('/',function(req, res) {
res.sendfile(__dirname + '/public/index.html');
});
我把所有的 html 放在 public 文件夹中,结构是这样的。
public/
-css folder
-angular dependencies
-all my htmls (index.html,login.html,main.html)
routes/
-index.js
views/
-index.ejs
-error.ejs
app.js
现在,当我击中我的基地 url 时,index.html 会根据我在上面给出的 app.js 中提供的代码进行加载。然后 angular 在 ng-view div 中插入 main.html 代码。当我点击 /login 时,我希望加载 login.html。但是加载的是相同的 main.html。有人可以阐明这一点吗?解决方案很少,但 none 帮了我的忙。
我想你想要类似 express.static
的东西app.use(express.static(__dirname + '/public'));
如果收到 "login.html" 的请求,它将首先检查 public 目录中的该文件。
How to use AngularJS routes with Express (Node.js) when a new page is requested?
这对我有帮助。
我做了更改
myApp.config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider){
$routeProvider
.when('/login', {
templateUrl : 'login.html',
controller: 'loginController'
}).when('/', {
templateUrl : 'main.html',
controller : 'mainController'
});
$locationProvider.html5Mode(true);
}]);
更改为 app.js
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.use('/',function(req, res) {
res.sendfile(__dirname + '/public/index.html');
});