相关疑难解决方法(0)

如何使用nodejs提供图像

我有一个位于public/images/logo.gif的徽标.这是我的nodejs代码.

http.createServer(function(req, res){
  res.writeHead(200, {'Content-Type': 'text/plain' });
  res.end('Hello World \n');
}).listen(8080, '127.0.0.1');
Run Code Online (Sandbox Code Playgroud)

它工作,但当我要求localhost:8080/logo.gif然后我显然我没有得到徽标.

我需要做些什么来改变图像.

node.js

133
推荐指数
7
解决办法
22万
查看次数

节点JS不提供静态图像

我已经在节点服务器上添加了用于提供图像的代码,但在连接到Node时似乎没有在HTML中提供图像.我还有正确提供的外部CSS和JS.这是Node中代码的片段(见下文).先感谢您!

var server = http.createServer(function(req, res) {
    var pathname = url.parse(req.url).pathname;
    var ext = path.extname(pathname);
    var handler = handlers[req.url];
    if(ext) {
        if(ext === ".css"){
            res.writeHead(200, { "Content-Type" : "text/css" });
        }
        else if(ext === ".js") {
            res.writeHead(200, { "Content-Type" : "text/javascript" });
        }
        else if(ext === ".jpg") {
            res.writeHead(200, { "Content-Type" : "image/jpg" });
        }
        res.write(fs.readFileSync(__dirname + pathname, "utf8"));
        res.end();
    }
    else if(handler) {
        handler(req, res);
    }
    else {
        res.writeHead(404, { "Content-type" : "text/plain" });
        res.end();
    }
});
server.listen(80);
Run Code Online (Sandbox Code Playgroud)

html javascript static-files node.js

2
推荐指数
1
解决办法
3523
查看次数

标签 统计

node.js ×2

html ×1

javascript ×1

static-files ×1