【发布时间】:2018-03-16 09:13:19
【问题描述】:
我使用 loopback 作为后端 api,使用 angular2 作为前端。
我正在使用环回服务我的 angular2 前端。
这很好用,但是,一旦我刷新页面,loopback 不知道如何处理 url,因为这是 angular 的工作,而不是 loopback。
所以我得到了这个错误:
我 100% 理解为什么会出现此错误,因为一旦 loopback 加载了我的 index.html,然后 angular2 就会被引导并知道如何处理这些类型的 URL,因为这是在我的 app.routing.ts 文件中指定的。但是,当直接访问这个链接时,angular2没有被引导,loopback也不知道如何处理这种类型的URL。
因此,我在 server.js 的环回代码中添加了代码,以将所有请求重定向到 Angular,但我用于环回的 /api 除外。
代码如下:
var path = require('path');
var ignoredPaths = ['/api'];
app.all('/*', function(req, res, next) {
//Redirecting to index only the requests that do not start with ignored paths
if(!startsWith(req.url, ignoredPaths))
res.sendFile('index.html', { root: path.resolve(__dirname, '..', 'client') });
else
next();
});
function startsWith(string, array) {
for(let i = 0; i < array.length; i++)
if(string.startsWith(array[i]))
return true;
return false;
}
这可行,但是 index.html 页面未加载,我收到以下控制台错误:
Refused to execute script from
'http://localhost:3000/inline.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
59074ce…:1 Refused to execute script from 'http://localhost:3000/polyfills.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
59074ce…:1 Refused to execute script from 'http://localhost:3000/scripts.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
59074ce…:1 Refused to execute script from 'http://localhost:3000/styles.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
59074ce…:1 Refused to execute script from 'http://localhost:3000/vendor.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
59074ce…:1 Refused to execute script from 'http://localhost:3000/main.bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
我了解该错误,但不知道为什么我会收到此错误,也不知道如何解决此问题。
这是我的环回后端的 middleware.json 文件:
{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
},
"helmet#xssFilter": {},
"helmet#frameguard": {
"params": [
"deny"
]
},
"helmet#hsts": {
"params": {
"maxAge": 0,
"includeSubdomains": true
}
},
"helmet#hidePoweredBy": {},
"helmet#ieNoOpen": {},
"helmet#noSniff": {},
"helmet#noCache": {
"enabled": false
}
},
"session": {},
"auth": {},
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {
"loopback#static": {
"params": "$!../client/"
}
},
"final": {
"loopback#urlNotFound": {}
},
"final:after": {
"strong-error-handler": {}
}
}
【问题讨论】:
-
我一直在尝试通过禁用一些安全性来尝试使用头盔选项,以查看它是否会起作用,但我仍然遇到同样的问题。
-
不要手动将问题标记为“已解决”。相反,您应该将答案标记为“已接受”。
-
@crashmstr 好的,我会在 2 天内完成