Resource interpreted as Stylesheet but transferred with MIME type text/html in AngularJS

只愿长相守 提交于 2019-12-05 06:32:27

问题


Have an angular application:

This is part of my index.html page:

<!DOCTYPE html>
<html lang="en" ng-app="ReApp">
<head>
    <meta charset="UTF-8">
    <title>REApp</title>
    <link rel="stylesheet" href="./lib/bootstrap/dist/css/bootstrap.min.css">
    <script src="./lib/angular/angular.min.js"></script>
</head>
<body></body>
</html>

This is part of my server.js(start point):

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use('/api', api);
app.get('*', function(req, res){
    res.sendFile(__dirname + '/public/app/views/index.html');
});

Problem is: when i have one route (localhost:3000/first) everything working. When i have route something like that: localhost:3000/first/second, i have an error "Resource interpreted as Stylesheet but transferred with MIME type text/html". In the terminal i have get request - first/second/lib/angular/angular.min.js. Of course i don't have that link. May who knows how to set a start page for this links in undex.html?


回答1:


To fix this issue all you have to do is place the <base href="/"> element just below the <title> element in the head section of index.html page.




回答2:


In my case, I was using packages installed by bower (the same should work for npm or yarn or any other package manager installed packages as well).

Add this middleware somewhere in your express code, before any other route declarations.

app.use('/assets', express.static(path.join(__dirname + '/bower_components')));

The first argument /assets is just a virtual path that doesn't have to exist in your project, and the second one is telling express to serve static assets from the bower_components folder. Next, in your markup, have your stylesheet inclusions like the following:

<link rel="stylesheet" href="/assets/bulma/css/bulma.min.css">


来源:https://stackoverflow.com/questions/32505432/resource-interpreted-as-stylesheet-but-transferred-with-mime-type-text-html-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!