express 静态文件夹中的样式 sheet
Style sheet in express static folder
我这样设置静态文件夹:
app.use(express.static(path.join(__dirname, '/public')));
并像这样引用样式 sheet(可以在 "public/stylesheets" 中找到):
<link rel="stylesheet" href="stylesheets/style.css">
它在 localhost:3500/ 和 localhost:3500/items 上工作正常,但是当我继续 localhost:3500/items/get 时,它会搜索样式 sheet 在 "items/stylesheets" 中。我应该怎么做才能使 "public" 库无论我身在何处都保持不变?
您缺少一个正斜杠,因此它总是相对于您网站的根目录进行搜索
<link rel="stylesheet" href="/stylesheets/style.css">
试试这个:
app.use('/public', express.static('public'));
我这样设置静态文件夹:
app.use(express.static(path.join(__dirname, '/public')));
并像这样引用样式 sheet(可以在 "public/stylesheets" 中找到):
<link rel="stylesheet" href="stylesheets/style.css">
它在 localhost:3500/ 和 localhost:3500/items 上工作正常,但是当我继续 localhost:3500/items/get 时,它会搜索样式 sheet 在 "items/stylesheets" 中。我应该怎么做才能使 "public" 库无论我身在何处都保持不变?
您缺少一个正斜杠,因此它总是相对于您网站的根目录进行搜索
<link rel="stylesheet" href="/stylesheets/style.css">
试试这个:
app.use('/public', express.static('public'));