如何使用node.js中的express框架提供图像文件?

时间:2022-08-02 18:28:25

In my application im using express framework to serve the client side files.But while giving background image for the html element.Its showing failed to load given url.

在我的应用程序中我使用快速框架来服务客户端文件。但同时为html元素提供背景图像。它显示无法加载给定的URL。

var express = require('express')
    , http  = require('http');

var app = express();
app.configure(function(){
    app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8000);

In public folder i have created javascripts,stylesheets,images folder.Now i'm getting javascripts and stylesheets.But i don't know how to access the image file.

在公共文件夹中,我创建了javascripts,样式表,图像文件夹。现在我正在获取javascripts和样式表。但我不知道如何访问图像文件。

.logo {
    background:url('localhost:8080\logo.jpg');//This image url not loading
    float:left;
    width:20px
    height:20px;
}

2 个解决方案

#1


20  

If your file directory is like

如果你的文件目录是这样的

/public
    /stylesheets
    /javascripts
    /images
        /logo.jpg

then your public access begins at the /public directory. This means that in order to access the image, the address would be localhost:8080/images/logo.jpg.

然后您的公共访问从/ public目录开始。这意味着为了访问图像,地址将是localhost:8080 / images / logo.jpg。

In summary, you had two problems.

总之,你有两个问题。

  1. Use a forward slash (/), not a backslash (\) in your URLs
  2. 在URL中使用正斜杠(/)而不是反斜杠(\)
  3. You missed including the image directory in the address
  4. 您错过了在地址中包含图像目录

#2


3  

You are also listening on port 8000, but the image you are referencing has port 8080.

您还在侦听端口8000,但您引用的映像具有端口8080。

#1


20  

If your file directory is like

如果你的文件目录是这样的

/public
    /stylesheets
    /javascripts
    /images
        /logo.jpg

then your public access begins at the /public directory. This means that in order to access the image, the address would be localhost:8080/images/logo.jpg.

然后您的公共访问从/ public目录开始。这意味着为了访问图像,地址将是localhost:8080 / images / logo.jpg。

In summary, you had two problems.

总之,你有两个问题。

  1. Use a forward slash (/), not a backslash (\) in your URLs
  2. 在URL中使用正斜杠(/)而不是反斜杠(\)
  3. You missed including the image directory in the address
  4. 您错过了在地址中包含图像目录

#2


3  

You are also listening on port 8000, but the image you are referencing has port 8080.

您还在侦听端口8000,但您引用的映像具有端口8080。