ループバックをバックエンドAPIとして、angular2をフロントエンドとして使用しています。loopbackからangular2へのリクエストを書き換えます(MIMEタイプ( 'text/html')が実行できないためスクリプトの実行を拒否しました)。
私はangle2フロントエンドにもサービスするためにループバックを使用しています。
これはうまくいきますが、一度ページを更新するとループバックでは角度のジョブでループバックではないため、URLの処理方法はわかりません。
だから私はこのエラーを取得する:
私はこのエラーを取得する理由をループバックは私のindex.htmlをロードしたら、その後、angular2はブートストラップとのこれらのタイプを処理する方法を知っているされているので、私は、100%理解これは私のapp.routing.tsファイルで指定されているためです。しかし、このリンクに直接アクセスすると、angular2はブートストラップされず、ループバックはこのタイプのURLの処理方法を知らない。
私はループバックに使用する/ api以外のすべてのリクエストをリダイレクトするために、server.jsのループバックコードにコードを追加しました。
がここにコードされています
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": {}
}
}
私はヘルメットのオプションを使って遊んでいますが、それが機能するかどうかを確認するためにいくつかのセキュリティを無効にしていますが、私はまだ同じことをしています。 – Janpan
問題を「解決済み」と手動でマークしないでください。代わりに、回答を「受け入れた」とマークする必要があります。 – crashmstr
@crashmstr ok、私は2日後になる – Janpan