Nodejs:如何返回具有相同响应(文本和图像)的不同内容类型? [英] Nodejs: How return different content types with same response (text and image)?

查看:175
本文介绍了Nodejs:如何返回具有相同响应(文本和图像)的不同内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习nodejs,我认为最好的方法是在不使用express或任何其他非核心模块的情况下尝试做一些事情。我坚持试图获得一些文本和图像同时传递。我正在尝试的代码是:

I'm trying to learn nodejs and I thought the best way to do this would be to try doing some stuff without using express or any other non-core modules. I'm stuck on trying to get some text and an image to be delivered simultaneously. The code I'm trying is:

var http = require('http');
var fs = require('fs');

var server = http.createServer(function(request,response) {

    fs.readFile('my_pic.jpg', function(error, file) {
        response.writeHead(200, {'content-type':'text/html'});
        response.write('<p>hi there!</p>');

        response.writeHead(200, {'content-type':'image/jpg'});
        response.write(file, 'image');

        response.end();
    });

});

var port = process.env.PORT || 8080;

server.listen(port, function() {
    console.log('Listening on port ' + port);
});

理想情况下应该交付的是:

So ideally what should be delivered is:

<html>
<body>
<p>hi there</p>
<img src='my_pic.jpg'/>
</body>
</html>

但相反,什么都没有出现。

But instead, nothing is appearing.

我写了'hi there'后显示文本,然后尝试交换文本和图片的位置(标题包括),显示图片,但我无法弄清楚如何同时显示两者,就像在真实的网页上一样。

I tried putting a response.end() after writing 'hi there', which displays the text, and after that I tried swapping the places of the text and picture (headers included), which displayed the picture, but I can't figure out how to get both to display simultaneously, like on a real web page.

你能解释一下吗?要将不同类型的内容放入同一网页 - 他们是否需要采用不同的回复?我在另一个问题上遇到的事情:

Can you explain how to go about putting in different types of content onto the same webpage - do they need to be in different responses? Something I came across on another question:

nodejs - 如何读取和输出jpg图像?

这样的东西看起来像解决方案,但我无法想象如何应用于我的情况(我在服务器方面没有很多经验。)

Something like that looks like the solution, but I can't figure out how to apply this to my situation (I don't have a lot of experience in the server side of things.)

非常感谢

编辑:从user568109的回复开始工作:

got it working from user568109's reply:

var http = require('http');
var fs = require('fs');


var server = http.createServer(function(request,response) {
fs.readFile('my_pic.jpg', function(error, file) {

    var imagedata = new Buffer(file).toString('base64');

    response.writeHead(200, {'content-type':'text/html'});
    response.write("hi there!<img src='data:my_pic.jpg;base64,"+imagedata+"'/>");
    response.end();

    });

});

var port = process.env.PORT || 8080;

server.listen(port, function() {
    console.log('Listening on port' + port);
});

这似乎在嵌入图像方面做了很多工作 - 如果我想放置多个图像在,我必须继续嵌套那些文件流回调?

This seems like a lot of work in embedding images though - if I wanted to put more than one image in, I'd have to just keep nesting those filestream callbacks?

我也尝试过这种方式,但它不起作用:

I also tried it this way but it doesn't work:

var http = require('http');
var fs = require('fs');

var server = http.createServer(function(request,response) {

    response.writeHead(200, {'content-type':'text/html'});
    response.write("hi there!<img src='my_pic.jpg'/>");
    response.end();

});

var port = process.env.PORT || 8080;

server.listen(port, function() {
    console.log('Listening on port' + port);
});

我不确定这是否是人们所说的

I wasn't sure if this was what people meant by the line

response.write('<img src="my_pic.jpg"/>')

,因为服务器似乎没有代表我做另一个请求来获取图像?它只显示一个损坏的图标图像。

, because the server doesn't seem to be making another request on my behalf to go fetch the image? It's just showing a broken icon image.

推荐答案

如果你做 response.write('< img src =my_pic.jpg/>'); 如上所述,只有当浏览器为图像发送GET时才会发送图像文件。这将成为多部分请求。

If you do response.write('<img src="my_pic.jpg"/>'); as mentioned above the image file would be sent only when browser sends GET for the image. It would become multi-part request.

或者你可以这样做。可以用HTML以二进制形式发送图像。使用:

Or you can do it like this. It is possible to send your image in binary form in HTML. Use :

<img src="data:image/gif;base64,imagedata">

其中imagedata是gif图像的base64编码。所以在node.js中执行此操作:

where imagedata is a base64 encoding of gif image. So do this in node.js :

//Write text in response.
content = get-image-file-contents;     //store image into content
imagedata = new Buffer(content).toString('base64');    //encode to base64
response.write('<img src="data:image/gif;base64,'+imagedata+'">');//send image
response.end();

点击此处查看正确的图片转换 NodeJS base64图像编码/解码不能正常工作

Check here for correct image conversion NodeJS base64 image encoding/decoding not quite working

这发送一个发送文本和图像的响应。响应只需要一个标题 response.writeHead(200,{'content-type':'text / html'});

This sends one response which sends both text and image. Only one header is required for response response.writeHead(200, {'content-type':'text/html'});

这篇关于Nodejs:如何返回具有相同响应(文本和图像)的不同内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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