如何在node.js中使用express框架来提供图像文件? [英] How to serve the image files using express framework in node.js?
问题描述
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);
在公共文件夹中,我创建了javascripts,stylesheets,images folder.Now我正在获取javascripts和stylesheets 。但是我不知道如何访问图像文件。
.logo {
background:url(' localhost:8080\logo.jpg'); //这个图像url不加载
float:left;
width:20px
height:20px;
}
如果您的文件目录是
/ public
/ stylesheets
/ javascripts
/ images
/ logo然后,您的公开访问从 / public $ c开始,
$ c>目录。这意味着为了访问图像,地址将是 localhost:8080 / images / logo.jpg
。
总而言之,您有两个问题。
- 使用正斜杠(
/
),而不是您的网址中的反斜杠(\
) - 您错过了
code>目录在地址
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.
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.
.logo {
background:url('localhost:8080\logo.jpg');//This image url not loading
float:left;
width:20px
height:20px;
}
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
.
In summary, you had two problems.
- Use a forward slash (
/
), not a backslash (\
) in your URLs - You missed including the
image
directory in the address
这篇关于如何在node.js中使用express框架来提供图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!