简体   繁体   English

尝试使用Express 4多次加载角度

[英]Tried to load angular more than once with express 4

When I try to load an angular route (and therefore a template) I get this error: 当我尝试加载角度路线(并因此加载模板)时,出现以下错误:

WARNING: Tried to load angular more than once. 警告:试图多次加载角度。

I think there's a problem with routing that's causing an infinite loop but I just can't work out what the issue is. 我认为路由存在问题,导致无限循环,但我无法弄清问题是什么。

server.js server.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express(); //Create the Express app

app.use(express.static(__dirname + '/public')); 

app.set('views', __dirname + '/server/views');

app.set('view engine', 'ejs');

module.exports = app;

app.js app.js

angular
  .module('vapescannerApp', [
    'ngAnimate',
    'ngResource',
    'ngRoute',
    'theControllers'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.ejs',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.ejs',
        controller: 'AboutCtrl'
      })
      .when('/something/:id', {
        templateUrl: 'views/ProductDetails.ejs',
        controller: 'DetailsCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

www.js www.js

var app = require('../server');

app.get('*', function(req, res){

    res.render("index");

});

app.set('port', process.env.PORT || 8000);

var server = app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + server.address().port);
});

App Structure 应用程式结构

-- bin
   - www.js
-- public
   - app.js
-- server
   -- views
      - productDetails.ejs
      - main.ejs
      - about.ejs
app.get('*', function(req, res){
    res.render("index");

It's hard to say exactly but this will redirect every route to render index . 很难确切地说,但这会将每条路由重定向到render index If you have a route that does not match a file in your public directory, express will attempt to render the index file again. 如果您的路由与公共目录中的文件不匹配,则express将尝试再次呈现索引文件。 If the index file is including Angular, it's possible that the inclusion is rendered twice. 如果索引文件包含Angular,则该包含可能会被渲染两次。 You can turn off this route or make it more specific for debugging purposes to confirm that all of your resources are being included properly. 您可以关闭此路由或使其更特定于调试目的,以确认所有资源均已正确包含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM