2017-04-13 84 views
0

我想能夠發送不同的路徑,如/ stuff和/ stuff/foo相同的文件,問題是,當我嘗試這樣做,我是收到以下錯誤:從任何路徑路由到相同的文件在快遞,nodejs

Refused to execute script from 'http://localhost:8000/stuff/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. 

下面是代碼:

app.use("/", express.static(path.join(__dirname, "./../public"))); 
app.get("/stuff", sendIndexHTML); 
app.get("/stuff/:id", sendIndexHTML); 

function sendIndexHTML(req, res) { 
    res.sendFile(path.join(__dirname + "./../public/index.html")); 
} 
+0

是您的應用SPA嗎? – Gntem

+0

@ Mr.Phoenix這是一個使用react-router的反應應用程序 – Ferus

+0

爲什麼不處理客戶端的路由?你已經使用react-router了,基本上你會渲染同一個頁面。 – Gntem

回答

2

你在你的index.html引用main.js爲相對路徑,所以它正從/stuff服務,這是路徑設置爲返回index.html(它有一個MIME類型text/html)。

你需要做的是更新你的HTML文件中的腳本文件路徑。

變化

<script src="main.js"></script> 

<script src="/main.js"></script> 

這樣,它是由被配置爲靜態資產的根路徑服務。

+0

修復它,謝謝! – Ferus