[英]Unable to load style and execute script Express JS
我有我的GET請求試圖使用以下命令在我的route.js文件中加載html頁面
app.get('/api/testresult', (req, res) => {
res.sendFile('/path/myfile.html');
});
另外,在我的server.js中,我有以下內容
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const path = require('path');
const port = 8000;
app.use('/', express.static(path.join(__dirname, './app/report')));
require('./app/routes')(app, {});
app.listen(port, () => {
console.log('Server running on ' + port);
});
但是,當我嘗試執行GET調用時,它會在瀏覽器控制台上引發錯誤
localhost/:1 Refused to apply style from
'http://localhost:8000/api/testresult/assets/app.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:2 GET http://localhost:8000/api/testresult/assets/app.js 404 (Not Found)
localhost/:1 Refused to execute script from 'http://localhost:8000/api/testresult/assets/app.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
我的文件結構是
MyApp
app
—report
—assets
—app.css
—app.js
—my file.html
—routes
—index.js
—routes.js
node_modules
package.json
server.js
我在這里想念什么?
因此,您的目錄層次結構在/assets/app.js
從您指向express.static()
中間件的位置顯示了app.js
因此,這就是您用來為其提供文件的URL,但是您顯然使用的URL路徑為/api/testresult/assets/app.js
。 那些根本不匹配。 該URL將在:
app/report/api/testresult/assets/app.js
但是,那不是文件所在的位置(因此您得到了404)。
您可以通過以下幾種方法修復它:
"/assets/app.js"
。 我猜您可能在客戶端文件中指定了"assets/app.js"
。 由於這是一個相對URL,因此瀏覽器會將其與您的網頁路徑結合起來,以構造URL,從而使服務器獲得對/api/testresult/assets/app.js
的請求。 app.use('/api/testresult', express.static(path.join(__dirname, './app/report')));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.