简体   繁体   中英

node.js express.static doesn't work properly

I have a project folder of the following structure:

Main Directory

  • 'client' directory: includes html, css and js files.
  • 'img' directory: includes images
  • server directory

Inside server directory I have a node.js file:

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'));
});

The index.html file "calls" css and js files that exist in 'client' directory and it also "calls" images that exist in 'img' directory.

However, when I open my browser and send a GET request a see an html page that can't read the images. I guess that something is wrong with

app.use('/', express.static(...));

but I don't know what. Can anyone help me figure out?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM