express.js不提供我的图片 [英] express.js not serving my image

查看:66
本文介绍了express.js不提供我的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这里出了什么问题.

I don't understand whats going wrong here.

目录结构:

app/server.js
app/public/index.html
app/public/js/main.js
app/public/img/car.png 

server.js

server.js

var fs = require('fs') ,express = require('express'),
app = express.createServer();

app.use(express.static(__dirname + "/public"));

app.get('/', function(req, res){
    fs.readFile(__dirname + '/public/index.html', 'utf8', function(err, text){
        res.send(text);
    });
});

app.listen(8080, function(){
    console.log('Server listening on %d', app.address().port);  
});

main.js

var marker = new google.maps.Marker({
        map:map,
        position:coords,
        icon: 'img/car.png'
    });

错误输出:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/img/car.png 

我所有的css文件和js文件加载都没有问题. 我在做什么错了?

All my css files and js files load with no problem. What am I doing wrong?

更新 这是由于文件名为car.png.png 在Windows中浏览时,文件扩展名不可见,所以我愚蠢地认为名称实际上是car.png 经验教训!

UPDATE This was due to the file being named car.png.png When browsing in windows, fileextensions were not visible so I was fooled into thinking the name was really car.png Lesson learned!

推荐答案

更改此行

app.use(express.static(__dirname + "/public"));

对此

app.use('/public', express.static(__dirname + "/public"));

这篇关于express.js不提供我的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆