我想獲得靜態文件(圖像,js,css),這些文件都在公共文件夾內,可用於我的所有路線。我只能將它們加載到index.html。 management.html文件無法訪問靜態文件,我在Chrome控制檯上收到了404錯誤。express.static只加載到index.html
文件夾結構:
-app/
---index.html
---management.html
---public/
------css/
------js/
------img/
server.js:
const express = require('express');
const path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile('index.html', {root: __dirname })
});
app.get('/management', function(req, res) {
res.sendFile('management.html', {root: __dirname })
});
app.listen(3000);
請顯示如何引用'management.html'中的文件 –
在您的HTML代碼中使用絕對路徑(以'/'開頭,而不是相對)。 – robertklep