我有一个以下结构的项目文件夹:
主目录
- 'client'目录:包括html,css和js文件。
- 'img'目录:包含图片
- 服务器目录
在服务器目录内我有一个node.js文件:
var path = require('path');
var express = require('express');
var app = express();
app.use('/', express.static(path.join(__dirname, '../img/')) );
app.use('/', express.static(path.join(__dirname, '../client/')) );
app.get('/', function(req,res) {
res.sendFile(path.join(__dirname, '../client/index.html'));
});
index.html文件“调用”存在于“client”目录中的css和js文件,它还“调用”“img”目录中存在的图像。
但是,当我打开浏览器并发送GET请求时,会看到无法读取图像的html页面。我想
有问题app.use('/', express.static(...));
但我不知道是什么。任何人都可以帮我搞清楚吗?
答案 0 :(得分:0)
Well... it seems that I've figured it out by myself.
Although the images are not in the same directory as html.index, the moment I use express.static, the content of the "imported" library is "brought" to the same directory of the node.js file.
Therefore, inside the html file, instead of writing:
<img src="../img/logo.png" id="logo">
I wrote:
<img src="logo.png" id="logo">
and it worked.