最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Routing error with angular.js and express - Stack Overflow

programmeradmin4浏览0评论

I am trying to do routing with angular.js around /parent root, which works if I'm using anchor links, etc (aka routing handled purely from angular. For instance, a link that has href of '/parent/createstudent' works. However, when I type this in the url bar or when I refresh it when it is already at that location, it generates an error since the backend route isn't done yet.

I thus tried a catchall route ('/parent/*') in express, but this is resulting in an error with all my js and css not being read. Does anyone know how to solve this?

My static file directory

public
  -> javascripts
      -> app.js
      -> angular.js
  -> stylesheets
  -> images

Error:

Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/jquery.js". home:1
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/angular.js". home:1
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/main.js". home:1
Uncaught SyntaxError: Unexpected token < jquery.js:1
Uncaught SyntaxError: Unexpected token < angular.js:1
Uncaught SyntaxError: Unexpected token < 

Express

app.get('/parent', function(req, res) {
  res.render('main')
});
app.get('/parent/*', function(req, res) {
  res.render('main')
});

Angular routing (i declare the controllers in the view itself)

var app = angular.module('app', []).config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/parent/login', 
    {
      templateUrl: '../templates/login.html'
    }).
    when('/parent/home', 
    {
      templateUrl: '../templates/home.html'
    }).
    otherwise(
    {
      redirectTo: '/parent'
    })
})

I am trying to do routing with angular.js around /parent root, which works if I'm using anchor links, etc (aka routing handled purely from angular. For instance, a link that has href of '/parent/createstudent' works. However, when I type this in the url bar or when I refresh it when it is already at that location, it generates an error since the backend route isn't done yet.

I thus tried a catchall route ('/parent/*') in express, but this is resulting in an error with all my js and css not being read. Does anyone know how to solve this?

My static file directory

public
  -> javascripts
      -> app.js
      -> angular.js
  -> stylesheets
  -> images

Error:

Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/jquery.js". home:1
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/angular.js". home:1
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/parent/javascripts/main.js". home:1
Uncaught SyntaxError: Unexpected token < jquery.js:1
Uncaught SyntaxError: Unexpected token < angular.js:1
Uncaught SyntaxError: Unexpected token < 

Express

app.get('/parent', function(req, res) {
  res.render('main')
});
app.get('/parent/*', function(req, res) {
  res.render('main')
});

Angular routing (i declare the controllers in the view itself)

var app = angular.module('app', []).config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/parent/login', 
    {
      templateUrl: '../templates/login.html'
    }).
    when('/parent/home', 
    {
      templateUrl: '../templates/home.html'
    }).
    otherwise(
    {
      redirectTo: '/parent'
    })
})
Share Improve this question asked Jun 8, 2013 at 12:05 Dan TangDan Tang 1,3432 gold badges21 silver badges38 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

This is a newly introduced "change of behavior" (or bug).

Try using the base tag :

<base href="/" />

You're serving your JS/CSS via /parent as well:

... http://localhost:3000/parent/javascripts/jquery.js
                         ^^^^^^^

So if you declare your routes before you declare the express.static middleware, your catch-all route will handle all JS/CSS requests.

You should use a setup similar to this:

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

// ...followed by your catch-all route (only one needed)
app.get('/parent*', function(req, res) {
  res.render('main')
});
发布评论

评论列表(0)

  1. 暂无评论